J2ME UI之窗口部件背景
主要设计意图是:使窗口跟部件跟活波,可以自己设置背景色等效果.
/**
* <b>类名:Background.java</b> </br>
* 编写日期: 2006-8-14 <br/>
* 程序功能描述:窗口,部件的基本背景类,子类可创建各式各样的背景,图片背景,半透明背景<br/>
* Demo: <br/>
* Bug: <br/>
*
* 程序变更日期 :<br/>
* 变更作者 :<br/>
* 变更说明 :<br/>
*
* @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a>
*/
public abstract class Background {
/**
* Defines the width of this Background.
* Usually this is 0, but some backgrounds might have a border included.
*/
protected int borderWidth;
protected int borderColor;
protected int backgroundColor;
/**
* Creates a new Background.
* The width of this background is set to 0 here.
*/
public Background() {
this.borderWidth = 0;
this.borderColor = 0;
this.backgroundColor = 0x7899999; //灰色
}
/**
* Paints this background.
*
* @param x the horizontal start point
* @param y the vertical start point
* @param width the width of the background
* @param height the height of the background
* @param g the Graphics on which the background should be painted.
*/
public abstract void paint( int x, int y, int width, int height, Graphics g );
}
/**
* <b>类名:LucencyBackground.java</b> </br> 编写日期: 2006-8-14 <br/> 程序功能描述:
* 创建具有透明度的背景<br/> Demo: <br/> Bug: <br/>
*
* 程序变更日期 :<br/> 变更作者:<br/> 变更说明 :<br/>
*
* @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a>
*/
public class LucencyBackground extends Background {
// 透明图片
private Image lucencyImage;
// 透明度
private int alpha ;
/**
* 设置背景色,跟透明度创建一个具体透明效果的背景
* @param _backgroundColor
* @param _alpha
*/
public LucencyBackground(int _backgroundColor, int _alpha ) {
backgroundColor = _backgroundColor;
alpha = _alpha ;
}
public void paint(int x, int y, int width, int height, Graphics g) {
initLucencyImage(width, height);
paint(x, y, g);
}
/*
* 初始话背景图片
*/
private void initLucencyImage(int width, int height) {
// 创建一个指定高,宽的可修改图片
lucencyImage = Image.createImage(width, height);
Graphics g = lucencyImage.getGraphics();
g.setColor(this.backgroundColor);
g.fillRect(0, 0, width, height);
// 画上面的横线
g.setColor(0xFFFFFF);
g.drawLine(1, 1, lucencyImage.getWidth(), 1);
// 画左边的白色竖线
g.setColor(0xFFFFFF);
g.drawLine(1, 1, 1, height);
g.setColor(0);
for (int i = 0; i < 3; i++) {
// 画右边的黑色竖线
g.drawLine(width-i, 0, width-i, height);
// 画底下的黑色横线
g.drawLine(0, height-i, width, height-i);
}
}
/*
* 创建RGB像素值
*/
private int[] createRGBInt() {
int argb[] = new int[lucencyImage.getWidth() * lucencyImage.getHeight()];
lucencyImage.getRGB(argb, 0, lucencyImage.getWidth(), 0, 0,
lucencyImage.getWidth(), lucencyImage.getHeight());// 获得图片的ARGB值
for (int i = 0; i < argb.length; i++) {
argb[i] = (alpha << 24) | (argb[i] & 0x00FFFFFF);// 修改最高2位的值
}
return argb;
}
/*
* 描绘出来
*/
private void paint(int x, int y, Graphics g) {
g.setColor(0);
//g.drawImage(lucencyImage, x, y, Graphics.TOP | Graphics.LEFT);
g.drawRGB(createRGBInt(), 0, lucencyImage.getWidth(), x, y,
lucencyImage.getWidth(), lucencyImage.getHeight(), true);// 画象素数组
}
}
测试类可以从上篇文章 <<J2ME UI之边框>>种的test获取,只需要修改
Border b = Border.getRectBorder(3,0x7899999);// TODO 自动生成方法存根
b.paint(5,5,70,30,g);
改为
LucencyBackground b = new LucencyBackground(0x7899999,30);
b.paint(5,5,100,30,g);
即可
以后设计按钮,菜单,窗口执行添加这些背景就可以有不同的表现形式了.^_^