我正在创建一个 J2ME 游戏,我注意到 getHeight() 函数的奇怪行为。
而不是以像素为单位给出全屏的高度,它似乎返回的比那个少
我的代码:
游戏画布.java:
package game1;
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
public class Gamecanvas extends GameCanvas implements Runnable{
private int W=getWidth(),H=getHeight();
public Gamecanvas(){
super(false);
}
//First called by the Midlet
public void start(){
setFullScreenMode(true);
Thread thread=new Thread(this);
thread.start();
}
public void run() {
System.out.println("Running Thread!");
Graphics(getGraphics());
}
private void Graphics(Graphics g) {
Colours(g);
}
private void Colours(Graphics g) {
int w=getWidth(),h=getHeight();
System.out.println("Correct height="+h);
System.out.println("Wrong Height="+H);
g.setColor(0xFFFF00);
g.fillRect(0, 0, w, h);
//If i write g.fillRect(0,0,W,H); the screen is not fully filled
flushGraphics();
}
}
放置时 int h=getHeight(); 在方法内部它返回 309 这是正确的,当放置 int H=getHeight(); 外部方法它返回 291 这是错误的
getWidth 没有问题。w 和 W 都返回 240。
那么,是什么导致了 getHeight() 的这种异常行为呢?