我正在尝试为游戏实现双缓冲,但我不确定我是否做得正确。
我this.repaint()
在我的主游戏循环中调用,然后应该这样做:
@Override
public void paint(Graphics g){
// this.getWidth() / this.getHeight() is the window size
this.dbImage = createImage(this.getWidth(), this.getHeight());
this.dbg = dbImage.getGraphics();
this.paintComponent(dbg);
g.drawImage(this.dbImage, 0, 0, this);
}
@Override
public void paintComponent(Graphics g){
try{
g.drawImage(bg, 0, 0, this);
// gameObjects is an ArrayList with an object in it that represents
// an item on the screen, such as an enemy or a bullet
for(int i = 0; i < gameObjects.size(); i++){
GameObject go = gameObjects.get(i);
g.drawImage(go.getSprite(), go.getX(), go.getY(), this);
}
}catch(Exception e){
}
}
我基本上做了这个人在这里所做的事情:YouTube 视频我遇到的问题是,它似乎比我有双缓冲之前运行得更糟。有时项目冻结半秒钟然后又恢复并正常运行,然后在几分钟(或更短)内再次发生。