| Finally, we use the most powerful method for eliminating flicker: 
              the double buffer.  The process of sending drawing instructions to the screen involves 
              a lot of overhead. There are lots of communications calls between 
              the AWT and the host that take time. So by doing the drawing all onto an image within Java and then 
              just sending the whole image in one call, there is a considerable 
              speed up in the drawing.  So below we create an image and then get its graphics 
              context object for doing the drawing onto it. Once this image is 
              ready, we just draw it to the screen in one call. Note that once a graphics context object is clipped, 
              it cannot be unclipped. So we need to get a new one each time around.  
              
                 
                  | DoubleBuffer_Applet7.java 
                      Resources: Apollo16Lander.jpg
 |   
                  | import 
                      java.awt.*;  public 
                      class DoubleBuffer_Applet7 extends Clipping_Applet7 {
 Image offScreenImage;
   
                      public void update( Graphics g ) {
 // Create an offscreen 
                      image and then get its
 // graphics context for the drawing.
 if ( offScreenImage == null )
 offScreenImage =
 createImage( getSize().width,
 getSize().height );
 
 Graphics gOffScreenImage= offScreenImage.getGraphics();
    
                      // Do the clipping on both the off // and on screen graphics contexts.
 int lastX = currentX, lastY = currentY;
 currentX = newX; currentY = newY;
 clipToAffectedArea( og, lastX, lastY,
 currentX, currentY, 
                      iamgeWd, imageHt );
 clipToAffectedArea( g, lastX, lastY,
 currentX, currentY, 
                      imageWd, imageHt );
    
                      // Now draw on the offscreen image. paint( gOffScreenImage 
                      );
    
                      // Don't bother to call paint, // just draw the offscreen image
 // to the screen.
 g.drawImage(offScrImg, 0, 0, this);
    
                      // Get rid of the offscreen graphics context.// Can't unclip a graphics context so 
                      have
 // to get a new one next time around.
 gOffScreenImage.dispose();
 }
 }
 |    The double buffering makes a big difference in reducing 
              the flicker of the image when it you move it with the mouse. Double 
              buffering is useful for any animation.  Note that Swing components already implement double 
              buffering so you don't need to do it yourself. However, clipping 
              may still help somewhat.  References & Web Resources Latest update: March 8, 2006 |