Java使用双缓冲消除图形闪烁现象

画图时一般先重写paint方法,再通过重写update方法消除闪烁:

调用过程repaint--->update--->paint

@Override
public void update(Graphics g) {
//1.得到缓冲图象
if(this.offScreenImage==null){
    this.offScreenImage = this.createImage(GAME_WIDTH,GAME_HEIGHT);
}
//2.得到缓冲图象的画笔
Graphics gOffScreen = this.offScreenImage.getGraphics();
//3.绘制缓冲图象
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.black);
gOffScreen.fillRect(0, 0, GAME_WIDTH,GAME_HEIGHT);
gOffScreen.setColor(c);
//4.调用paint(),将缓冲图象的画笔传入
paint(gOffScreen);
//5.再将此缓冲图像一次性绘到代表屏幕的Graphics对象,即该方法传入的“g”上
g.drawImage(offScreenImage, 0, 0, null);
}

你可能感兴趣的:(Java使用双缓冲消除图形闪烁现象)