import java.awt.*; import java.awt.image.*; import java.awt.geom.*; import java.util.Date; /* A simple demonstration for the use of double buffering on the basis of the earlier geometric transforms example. In addition to the rotating and changing rectangle, we have a static blue ellipse in the background. */ public class DoubleBuffering extends Frame { //This buffered image will contain the background that remains unchanged. //Note that the package java.awt.image.* is needed. private BufferedImage theBackground; //On this buffered image we will first paint the background and then //the moving shape (the rectangle). Finally we draw this image on the //actual screen. private BufferedImage theImage; private Graphics2D g2dBackground; private Graphics2D g2dImage; public DoubleBuffering() { addWindowListener(new MyFinishWindow()); initialiseTheBackgroundAndTheImage(); } public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; int xul, yul, xwidth, yheight; xul = 200; //x-coordinate of the upper left corner of the rectangle yul = 300; //y-coordinate of the upper left corner of the rectangle xwidth = 100; //width of the rectangle yheight = 20; //height of the rectangle //midpoint of the rectangle double xmid = xul+xwidth/2.0; double ymid = yul+yheight/2.0; //stretching/shrinking factors double xstretch = 1.005; double ystretch = 0.995; //number of (rotation/stretching) steps to be carried out int steps = 360; //this shape will contain the transformed rectangle Shape s; //create the rectangle Rectangle2D.Double rect = new Rectangle2D.Double(xul,yul,xwidth,yheight); AffineTransform id = new AffineTransform(); AffineTransform rotateBy1Degree = new AffineTransform(); rotateBy1Degree.rotate(Math.PI/180,xmid,ymid); //this transform will contain the accumulated rotation AffineTransform accRotation = new AffineTransform(); AffineTransform stretch = scalingWRTXY(xmid,ymid,xstretch,ystretch); //this transform will contain the accumulated scaling AffineTransform accStretch = new AffineTransform(); //draw the initial rectangle s = id.createTransformedShape(rect); //first draw the background on the buffered image g2dImage.drawImage(theBackground,0,0,null); //then draw the actual rectangle on the buffered image g2dImage.fill(s); //finally draw the buffered image on the screen g2d.drawImage(theImage,0,0,null); //wait for some time sustain(1000); for (int i=0; i