作者:alexhy
为了用标准的SUN
WTK2.1开发能兼容各种机型的MIDP2.0游戏,会遇到一个奇怪的全屏问题,在K700上好好的,到了Nokia上居然死活显示一半,又不想在
Eclipse里改用很不爽的Nokia
wtk(配置麻烦,有中文问题,启动也慢),费了一番功夫后,终于不用FullCanvas实现了兼容K700和Nokia的全屏显示,方法如下:
在
索爱的K700上,实现全屏很简单,如下写法就可以了: public Canvas1() { super(false);
this.setFullScreenMode(true); this.width=getWidth();
this.height=getHeight(); } 得到的屏幕大小是176*220。
Nokia上就比较奇怪了,这种方式取得的
width和height居然是176*144。需要加以下代码修正一下: if(width>=176) {
if(height<208) { height=208; } }
绘图部分都以width和height为基准。另外canvas1不能继承系统的GameCanvas,因为系统GameCanvas里的缓冲图还是
176*144的,画出来就只能是半屏,我用j2me
polish里的GameCanvas修改一下后,放到src里代替系统GameCanvas就OK了。
修正过的GameCanvas代码如下:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public abstract class GameCanvas extends Canvas{
public static final int UP_PRESSED = 0x0002;
public static final int DOWN_PRESSED = 0x0040;
public static final int LEFT_PRESSED = 0x0004;
public static final int RIGHT_PRESSED = 0x0020;
public static final int FIRE_PRESSED = 0x0100;
public static final int GAME_A_PRESSED = 0x0200;
public static final int GAME_B_PRESSED = 0x0400;
public static final int GAME_C_PRESSED = 0x0800;
public static final int GAME_D_PRESSED = 0x1000;
protected int keyStates;
protected int releasedKeys;
private Image bufferedImage;
private int clipX, clipY, clipWidth, clipHeight;
private boolean setClip;
protected GameCanvas(boolean suppressKeyEvents) {
super();
this.setFullScreenMode(true);
int width = getWidth();
int height = getHeight();
if (width >= 176) {
if (height < 208) {
height = 208;
}
}
this.bufferedImage = Image.createImage(width, height);
}
protected Graphics getGraphics() {
return this.bufferedImage.getGraphics();
}
public int getKeyStates() {
int states = this.keyStates;
this.keyStates &= ~this.releasedKeys;
this.releasedKeys = 0;
return states;
}
public void paint(Graphics g) {
if (this.setClip) {
g.clipRect(this.clipX, this.clipY, this.clipWidth, this.clipHeight);
this.setClip = false;
}
g.drawImage(this.bufferedImage, 0, 0, Graphics.TOP | Graphics.LEFT);
}
public void flushGraphics(int x, int y, int width, int height) {
this.setClip = true;
this.clipX = x;
this.clipY = y;
this.clipWidth = width;
this.clipHeight = height;
repaint();
serviceRepaints();
}
public void flushGraphics() {
repaint();
serviceRepaints();
}
protected void keyPressed(int keyCode) {
int gameAction = KeyMapping.getGameKey(keyCode);
if (gameAction != 0) {
int bit = 1 << gameAction;
this.keyStates |= bit;
this.releasedKeys &= ~bit;
}
}
public void setFullScreenMode(boolean enable) {
super.setFullScreenMode(enable);
}
protected void keyReleased(int keyCode) {
int gameAction = KeyMapping.getGameKey(keyCode);
if (gameAction != 0) {
this.releasedKeys |= 1 << gameAction;
}
}
}
以上方法适用于MIDP2.0,标准的MIDP1.0里不支持this.setFullScreenMode(true)命令,只有用FullCanvas了。