j2me程序中,如果我们先前程序设置的屏幕大小为240X300,为了在240X320屏幕绘制时自适应居中我们可以使用双缓冲来实现
gamescreen中
定义变量:
public final static int screenW = 240; //程序原来的宽度
public final static int screenH = 300;//程序原来的高度
public static int screenHnew = 0;
public static int offsetHeight = 0; //-----动态改变的
Image offImage;//先用一个image的来画,然后就再画在目标设置的上的指定位置.
Graphics g;
初始化 构造函数
testHeight = this.getHeight();
//注意:某些手机得到的高度不一定准确,开始前要测试下this.getHeight()方法得到手机的实际屏幕大小是多少。如nokia 6680得到的高度就不是208
offsetHeight =(screenHnew -screenH)/2;
if (screenHnew != screenH)
{
offImage=Image.createImage(screenW,screenH);
}
paint(Graphics gBuffer)的开始
if (screenHnew != screenH)
{
g=offImage.getGraphics();
}
else
{
g = gBuffer;
}
paint(Graphics gBuffer)的结束部分
if (screenHnew != screenH)
{
gBuffer.setColor(0,0,0);
gBuffer.fillRect(0,0,screenW,screenHnew );
gBuffer.setClip(0,offsetHeight ,screenW,screenH);
gBuffer.drawImage(offImage,0,offsetHeight ,Graphics.TOP|Graphics.LEFT);
gBuffer.setClip(0,0,screenW,screenHnew );
}