Home : Course Map : Chapter 11 : Java : Supplements :
Reducing Flicker 1 : Override Update
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

The example below illustrates the flickering problem. Moving the image with the mouse causes both the background and the image to flicker.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class FlickerMax_Applet7 extends Applet
   implements MouseMotionListener
{
  int numBands = 12;
  int currentX, currentY;
  Image image;

  public void init()
  {
    String imageName = getParameter("ImageName");
    if(imageName == null) imageName = "Apollo16Lander.gif";
    String numBandStr = getParameter("NumBands");
    if( numBandStr != null)
        numBands = Integer.parseInt(numBandStr);

    img=getImage(getCodeBase(),"monaSmall.gif" );
    addMouseMotionListener(this);
  }

  // Get the mouse dragging coordinates.
  public void mouseDragged( MouseEvent e ) {
    currentX = e.getX();
    currentY = e.getY();
    repaint();
  }
  // Override mouseMoved with empty method.
  public void mouseMoved ( MouseEvent e){}
 
  public void paint( Graphics g ) {
    int wd = getSize().width;
    int ht = getSize().height;
    int wBand = wd/numBands;

    boolean changeBand = false;

    // Draw stripped background
    int xStart = 0;
    for ( int y = 0; y < ht; y++ )
    { changeBand = false;
      for ( int x = xStart; x < wd; )
      { g.setColor(
          (changeBand = !changeBand) ? Color.RED : Color.BLUE );
         g.fillRect( x, y, wBand, 1); x += wBand; }
         xStart -= 1; if( (-xStart) > wBand)
         xStart = 0;
    }

    // then the image
    g.drawImage( image, currentX, currentY, this );
  }
}

 

After repaint() is called, a request for drawing is put into a queue in the AWT graphics engine. When the request is process, the paint() method is not called directly. Instead, the update() method is invoked. This method clears the component face by drawing a rectangle the size of the component with the background color.

Since in this case, we redraw the whole applet anyway, there is no need for update() to waste time clearing it. So the following apple overrides update() with a version that just calls paint():

Update_Applet7.java
Resources: Apollo16Lander.jpg

import java.awt.*;
import java.awt.event.*;
java.applet.*;

public class Update_Applet7 extends Applet

...
... 
  // Override update to prevent clearing of
  // background since the whole frame will be
  // drawn over anyway.

  public void update ( Graphics g )
  { paint(g); }
 
  public void paint( Graphics g )
  {
...

 

 

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.