import java.applet.Applet;
import java.awt.*;

public class ImageAnimation extends Applet {

/** Sequence through an array of 15 images to perform the
 *  animation. A separate Thread controls each tumbling Duke.
 *  The Applet's stop method calls a public service of the
 *  Duke class to terminate the thread. Override update to
 *  avoid flicker problems.
 *
 *  Taken from Core Web Programming from 
 *  Prentice Hall and Sun Microsystems Press,
 *  http://www.corewebprogramming.com/.
 *  &copy; 2001 Marty Hall and Larry Brown;
 *  may be freely used or adapted. 
 */

  private static final int NUMDUKES  = 2;
  private Duke[] dukes;
  private int i;

  public void init() {
    dukes = new Duke[NUMDUKES];
    setBackground(Color.white);
  }


  /** Start each thread, specifing a direction to sequence
   *  through the array of images.
   */

  public void start() {
    int tumbleDirection;
    for (int i=0; i<NUMDUKES ; i++) {
      tumbleDirection = (i%2==0) ? 1 :-1;
      dukes[i] = new Duke(tumbleDirection, this);
      dukes[i].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);
  }

  public void paint(Graphics g) {
    for (i=0 ; i<NUMDUKES ; i++) {
      if (dukes[i] != null) {
        g.drawImage(Duke.images[dukes[i].getIndex()],
                    200*i, 0, this);
      }
    }
  }


  /** When the Applet's stop method is called, use the public
   *  service, setState, of the Duke class to set a flag and
   *  terminate the run method of the thread.
   */

  public void stop() {
    for (int i=0; i<NUMDUKES ; i++) {
      if (dukes[i] != null) {
        dukes[i].setState(Duke.STOP);
      }
    }
  }
}