Single-Thread Animation

1. Idea

Perform animation by loading a sequence of images into an array and cycling through the images via a while loop in the thread. Each time the thread loops through the while loop, the array index is incremented and repaint (which triggers update) is called to update the images on the screen. The thread’s run method is stopped by setting a flag in the applet’s stop method. In this case the thread varuiable is set to null to facilitate a flag.

2.  AnimationOneThread.java: (Download Source)

import java.applet.Applet;
import java.awt.*;

public class AnimationOneThread extends Applet
                                implements Runnable {

//----------------------------------------------------
/** Sequence through an array of 15 images.
 *  A thread controls the image index to display.
 *  Overrides update to avoid flicker problems.
 */

  private static final int numImages = 15;
  private Image[] images;
  private int     index;
  private Thread  Duke = null;

  public void init() {
    // load images
    images = new Image[ numImages ];
    for ( int i=0; i<numImages ; i++ )
      images[i] = getImage( getCodeBase(),
                         "images/T" + i + ".gif");
    setBackground( Color.white );
  }

  public void start() {
    if (Duke == null) {
      Duke = new Thread(this);
      Duke.start();
    }
  }

  //----------------------------------------------------
  /** Skip the usual screen-clearing step of update
   *  so that there is no "flicker" between each 
   *  drawing step.
   */
  public void update(Graphics g) {
    paint(g);
  }

  //----------------------------------------------------
  /** Draw corresponding Duke image on left part of 
   *  applet and right part of applet.
   */
  public void paint(Graphics g) {
    g.drawImage(images[ index ], 0, 0, this);
    g.drawImage(images[ numImages - index -1 ],
                                        200, 0, this);
  }


  //----------------------------------------------------
  /** The index into image array is incremented
   *  once each time through the while loop, call
   *  repaint, and pauses for a moment. The
   *  paint method will perform image drawing.
   *  While loop will terminate when Duke is set to 
   *  null.
   */
  public void run() {
    index = -1;
    Thread myThread = Thread.currentThread();
    while (Duke == myThread) { // While Duke not null
      if ( ++index >= numImages ) index = 0;
        repaint();
        try { 
          Thread.sleep(100); 
        } catch ( InterruptedException e )
        { break; }  // break while loop
    }
  }

  //----------------------------------------------------
  /** When the Applet's stop method will be called Duke
   *  will be set to null as a flag to stop the while
   *  loop in run method.
   */
  public void stop() {
    if (Duke != null) {
      Duke = null;
    }
  }
}


3. Result (Download Source)

Single Thread Animation

Error! You must use a Java enabled browser.

© 1996-2000 Marty Hall,  © 1999-2000 Lawrence M. Brown