1 package JSci.awt; 2 3 import java.awt.*; 4 5 18 public abstract class DoubleBufferedCanvas extends Canvas { 19 private Image buffer = null; 20 private boolean doRedraw = true; 21 24 public DoubleBufferedCanvas() {} 25 29 public final void paint(Graphics g) { 30 if(doRedraw) { 31 doRedraw = false; 32 final int width=getSize().width; 33 final int height=getSize().height; 34 buffer=createImage(width,height); 35 if(buffer == null) 36 return; 37 final Graphics graphics=buffer.getGraphics(); 38 39 Color oldColor = graphics.getColor(); 40 graphics.setColor(getBackground()); 41 graphics.fillRect(0,0,width,height); 42 43 graphics.setColor(oldColor); 44 offscreenPaint(graphics); 45 } 46 g.drawImage(buffer, 0, 0, null); 47 } 48 51 public final void update(Graphics g) { 52 paint(g); 53 } 54 57 public final void print(Graphics g) { 58 offscreenPaint(g); 59 } 60 64 public final void redraw() { 65 doRedraw = true; 66 repaint(); 67 } 68 71 protected final Graphics getOffscreenGraphics() { 72 return (buffer != null) ? buffer.getGraphics() : null; 73 } 74 78 protected abstract void offscreenPaint(Graphics g); 79 } 80 81 | Popular Tags |