我正在开发一款小型街机视频游戏,并且我正在寻找双缓冲以改善动画效果。我有一个类应该绘制空白图像,另一个类应该绘制一条简单的线。但是,我一直在应该绘制线的线上收到 NullPointerException
class Render extends JPanel {
public int dbWidth = 500, dbHeight = 400;
public Image dbImage = null;
public Graphics dbg;
public void gameRender() {
if( dbImage == null )
dbImage = createImage( dbWidth, dbHeight );
dbg = dbImage.getGraphics();
dbg.setColor( Color.white );
dbg.fillRect( 0, 0, dbWidth, dbHeight );
}
}
class MC extends Render {
public Render render = new Render();
public void draw() {
render.gameRender();
dbg.drawLine( 100, 100, 200, 200 ); // line where NullPointerException occurs
}
}
我想是 Graphics 变量 dbg 为空,但它获得了dbImage.getGraphics();
如何gameRender();
修复此 NullPointerException?
我也在像这样的另一个类中调用 draw() 方法
public void run() {
running = true;
while( running ) {
mc.draw();
try {
Thread.sleep( 50 );
}
catch( Exception e ) {}
}
}
我在那个类的构造函数中说过 mc = new MC();