雙緩沖,我記得有兩種方法,一種是兩個圖象,一個前臺Graphics,一個后臺Graphics,畫圖時在后臺畫,畫好了,再用前臺的Graphics畫后臺Graphics的圖片。另一種是有兩個或多個圖象,不分前后臺,顯示一個圖象時,另外的在畫,圖象循環使用(c語言時用過)。
給出第一種的java實現
a、 定義后臺圖象BufferedImage offScreenImg=offScreenImg = new BufferedImage(this.maxX, this.maxY,
BufferedImage.TYPE_INT_RGB);
b、 得到后臺的Graphics實例
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);
}
繪制時用后臺Graphics,繪制好后,調用repaint(),將offScreenImg繪到面板上。