雙緩沖,我記得有兩種方法,一種是兩個(gè)圖象,一個(gè)前臺(tái)Graphics,一個(gè)后臺(tái)Graphics,畫(huà)圖時(shí)在后臺(tái)畫(huà),畫(huà)好了,再用前臺(tái)的Graphics畫(huà)后臺(tái)Graphics的圖片。另一種是有兩個(gè)或多個(gè)圖象,不分前后臺(tái),顯示一個(gè)圖象時(shí),另外的在畫(huà),圖象循環(huán)使用(c語(yǔ)言時(shí)用過(guò))。
給出第一種的java實(shí)現(xiàn)
a、 定義后臺(tái)圖象BufferedImage offScreenImg=offScreenImg = new BufferedImage(this.maxX, this.maxY,
BufferedImage.TYPE_INT_RGB);
b、 得到后臺(tái)的Graphics實(shí)例
Graphics2D offScreenG;
offScreenG = offScreenImg.createGraphics();
c、 覆蓋paintComponent方法
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(offScreenImg, 0, 0, this.maxX, this.maxY, this);
}
繪制時(shí)用后臺(tái)Graphics,繪制好后,調(diào)用repaint(),將offScreenImg繪到面板上。