1 package JSci.swing; 2 3 import java.awt.*; 4 import javax.swing.*; 5 6 19 public abstract class JDoubleBufferedComponent extends JComponent { 20 private Image buffer = null; 21 private boolean doRedraw = true; 22 25 public JDoubleBufferedComponent() { 26 super.setDoubleBuffered(false); 27 } 28 32 public final void paintComponent(Graphics g) { 33 Insets insets = getInsets(); 34 if(doRedraw) { 35 doRedraw = false; 36 final int width = getWidth()-insets.left-insets.right; 37 final int height = getHeight()-insets.top-insets.bottom; 38 buffer = createImage(width, height); 39 if(buffer == null) 40 return; 41 final Graphics graphics=buffer.getGraphics(); 42 43 Color oldColor = graphics.getColor(); 44 graphics.setColor(getBackground()); 45 graphics.fillRect(insets.left, insets.top, width, height); 46 47 graphics.setColor(oldColor); 48 offscreenPaint(graphics); 49 } 50 g.drawImage(buffer, insets.left, insets.top, null); 51 } 52 55 public final void update(Graphics g) { 56 paint(g); 57 } 58 61 public final void printComponent(Graphics g) { 62 offscreenPaint(g); 63 } 64 68 public final void setDoubleBuffered(boolean flag) { 69 throw new IllegalArgumentException (); 70 } 71 75 public final boolean isDoubleBuffered() { 76 return true; 77 } 78 82 public final void redraw() { 83 doRedraw = true; 84 repaint(); 85 } 86 89 protected final Graphics getOffscreenGraphics() { 90 return (buffer != null) ? buffer.getGraphics() : null; 91 } 92 96 protected abstract void offscreenPaint(Graphics g); 97 } 98 99 | Popular Tags |