爲了用標準的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了。