Home : Course Map : Chapter 11 : Java : Supplements :
Reducing Flicker 3 : Double Buffer
JavaTech
Course Map
Chapter 11

Introduction
Image Class
Image Loading
  Demo 1 Demo 2  
Pixels/Transparency
  Demo 3
Pixel Handling
  Demo 4  
Demo 5
Exercises

    Supplements
Java 2D Imaging
BufferedImage
Creating Buf.Image
Pixel Handling
  Demo 1 Demo 2
Filters
  Convolutions
     Demo 3
  AffineTransforms
     Demo 4
  LookupTable
     Demo 5 Demo 6
  Rescale
     Demo 7
  Color Conversion
     Demo 8
  Custom
     Demo 9
Exercises
Java Adv Imaging
AWT Flicker:
  Override Update
     Demo 1  Demo 2
  Clipping
     Demo 3
  Double Buffer
     Demo 4

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

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

              Tech
Fractals
Fractal Drawing
   Demo 1
Fractal Draw Code
Fractal Images
  Demo 2
Image Processing
  Demo 3
Histogram Image
  Demo 4
Exercises

           Physics
Calibration/SysError
SimWithCal/SysErr
  Demo 1
Analysis
  Demo 2
Examples

Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.