/* * DoubleBuffer.java * * Created on June 19, 2003, 2:30 PM */ import java.awt.*; /** Test program for double-buffered graphics. * Run with no command-line arguments. * Obscure and un-obscure the window to cause a paint with no paintBuffer. * Re-size the window to cause a paintBuffer. * * @author scott gilbertson */ public class DoubleBuffer extends Component { private int bufferWidth; private int bufferHeight; private Image bufferImage; private Graphics2D bufferGraphics; boolean redrawRequired = true; /** Creates new DoubleBufferedPanel */ public DoubleBuffer () { super (); setBackground (Color.white); } int paintNumber=0; final public void paint (Graphics g) { System.out.println ("Paint #" + (++paintNumber) + " for " + this ); // checks the buffersize with the current panelsize // or initialises the image with the first paint if(bufferWidth!=getSize ().width || bufferHeight!=getSize ().height || bufferImage==null || bufferGraphics==null) resetBuffer (); if(bufferGraphics!=null) { if (redrawRequired) { redrawRequired = false; // this clears the offscreen image, not the onscreen one bufferGraphics.clearRect (0,0,bufferWidth,bufferHeight); // calls the paintbuffer method with the offscreen graphics as a param paintBuffer (bufferGraphics); } // we finaly paint the offscreen image onto the onscreen image g.drawImage (bufferImage,0,0,this); } else paintBuffer (g); } /** in classes extended from this one, add something to paint here! * @param g The graphics (off-screen) onto which to draw */ public void paintBuffer (Graphics g) { super.paint (g); } /** Mark the graphics as "dirty", so that they'll be re-drawn on the * next repaint. */ public void setRedrawRequired () { redrawRequired = true; } private void resetBuffer () { // always keep track of the image size bufferWidth=getSize ().width; bufferHeight=getSize ().height; // clean up the previous image if(bufferGraphics!=null) { bufferGraphics.dispose (); bufferGraphics=null; } if(bufferImage!=null) { bufferImage.flush (); bufferImage=null; } // create the new image with the size of the panel bufferImage=createImage (bufferWidth,bufferHeight); bufferGraphics=(Graphics2D)(bufferImage.getGraphics ()); bufferGraphics.setBackground (Color.white); bufferGraphics.setColor (Color.black); Class imageClass = bufferImage.getClass (); System.out.println ("bufferImage class is " + imageClass.getName ()); while (imageClass != null) { imageClass = imageClass.getSuperclass (); if (imageClass != null) { System.out.println (" - " + imageClass.getName ()); } } System.out.println ("bufferGraphics class is " + bufferGraphics.getClass ().getName ()); setRedrawRequired (); } final public void update (Graphics g) { paint (g); } public static void main (String[] args) { System.out.println ("Testing double-buffered graphics"); Frame frame = new Frame (); frame.addWindowListener (new java.awt.event.WindowAdapter () { public void windowClosing (java.awt.event.WindowEvent evt) { System.exit (0); } }); DoubleBufferedPanel panel = new DoubleBufferedPanel () { int repaintCount = 0; public void paintBuffer (Graphics g) { g.setColor (Color.black); g.drawString ("paintBuffer #" + (++repaintCount),20,20); } }; frame.add (panel); frame.pack (); frame.setVisible (true); } /** Returns the component's preferred size. * * @return the component's preferred size * @see #getMinimumSize() * @see LayoutManager */ public Dimension getPreferredSize () { final Dimension retValue = new Dimension (300,200); return retValue; } /** Returns the component's maximum size. * * @return the component's maximum size * @see #getMinimumSize() * @see #getPreferredSize() * @see LayoutManager */ public Dimension getMaximumSize () { final Dimension retValue = new Dimension (Integer.MAX_VALUE,Integer.MAX_VALUE); return retValue; } /** Returns the component's minimum size. * * @return the component's minimum size * @see #getPreferredSize() * @see LayoutManager */ public Dimension getMinimumSize () { final Dimension retValue = new Dimension (5,5); return retValue; } }