我正在写一个简单的游戏。我有三个类,第一个是:处理所有涉及到的东西的球,第二个是由一系列“球”组成的游戏,最后一个是包含主线程的窗口。Java - 双缓冲NullPointerException
window.paint调用game.draw以便接收游戏场景的图形。游戏本身加倍缓冲它,以便Image对象可以移动到玩家的球位置(尚未实现)。
所以我的问题,因为我创建一个图像对象,但为什么这初始化为null,因此我得到NullPointerException。
下面是其处理的绘画方法的源:
public class MyWindow extends JFrame {
//...the other code
public void paint(Graphics g){
thegame.draw();
repaint();
}
}
public class Game extends JFrame implements Runnable {
ball[] cellmap;
//...the other code
public void draw(){
Image GameImage = createImage(800,800);
Graphics GameGraphics = GameImage.getGraphics();
for(int i = 0;i<cellmap.length;i++)
cellmap[i].draw(GameGraphics);
g.drawImage(GameImage, 0, 0, this);
}
}
public class Ball extends JFrame {
//...the other code
public void draw(Graphics g){
g.setColor(Color.red);
g.fillOval((int)(this.x+this.radious),(int)(this.y+this.radious),
(int)this.radious,(int)this.radious);
}
}
请告诉我你不实际使用lowerCamelCase类和UpperCamelCase变量。 – 2012-02-06 17:17:02
可能是相关的:http://stackoverflow.com/questions/1541845/problems-with-createimageint-width-int-height – 2012-02-06 17:18:35
这个代码在引入第二个'JFrame'后就开始出错了,更不用说第三个了!为了更快地获得更好的帮助,请发布[SSCCE](http://sscce.org/)。 – 2012-02-06 23:08:27