// Filename:    Animator.java
// Coder:       Cuong Do
// Description: Simple animation class.  Basically, this retrieves a series of
//              graphic files from a server.  Adapted from Java in a Nutshell.

import java.applet.*;
import java.awt.*;
import java.net.*;
import java.util.*;

public class Animator extends Applet implements Runnable {
  protected Image[] images;
  protected int numImages;
  protected int currentImage;
  protected MediaTracker tracker;

  // the thread that runs the animation
  private Thread animator_thread = null;
//  private boolean threadSuspended = false;
  private int iteration;

  public boolean mouseDown(Event e, int x, int y) {
		start();
//        if (threadSuspended) {
//            // animator_thread.resume();
//			start();
//            }
//        else {
//            // animator_thread.suspend();
//			stop();
//            }
//        threadSuspended = !threadSuspended;
        return true;
    }

  // (1) Read the basename and numimages parameters from HTML files
  // (2) Read in the images, using base filename.  For example, if you specify
  //     the base filename image, the resulting filenames will be: image0, 
  //     image1, image2, etc.
  public void init() {
    String basename = this.getParameter("basename");
    try { 
      numImages = Integer.parseInt(this.getParameter("numimages")); 
    }
    catch (NumberFormatException e) { 
      numImages = 0; 
    }

    // getImage() creates an Image object from a URL specification,
    // but it doesn't actually load the images; that is done 
    // asynchronously.  Store all the images in a MediaTracker
    // so we can wait until they have all loaded (in run()).
    tracker = new MediaTracker(this);
    images = new Image[numImages];
    for(int i = 0; i < numImages; i++) {
      images[i] = this.getImage(this.getDocumentBase(), basename + i);
      tracker.addImage(images[i], i);
    }
  }

  
  public void start() {
	iteration = 3 * numImages;
    if (animator_thread == null) {
      animator_thread = new Thread(this);
      animator_thread.start();
    }
  }
    
  public void stop() {
    if ((animator_thread != null) && animator_thread.isAlive()) 
      animator_thread.stop();

    animator_thread = null;
  }
    
  // the body of the thread -- this does the animation
  public void run() {
    // First, force all the images to be loaded, and wait until
    // they have all loaded completely.
    for (int i = 0; i < numImages; i++) {
      this.showStatus("Loading image: " + i);
      // The argument is the same one passed to addImage()
      try { tracker.waitForID(i); } catch (InterruptedException e) { ; }
      // Check for errors loading it.
      if (tracker.isErrorID(i)) {
        this.showStatus("Error loading image " + i + "; quitting.");
        return; 
      }
    }
    
    this.showStatus("Loading images: done.");
        
    // actual  animation
    while(iteration-- > 0) {
      if (++currentImage >= images.length) currentImage = 0;
        this.getGraphics().drawImage(images[currentImage], 0, 0, this);
      this.getToolkit().sync();  // Force it to be drawn *now*.
      
      try { 
        Thread.sleep(50); 
      } 
      catch (InterruptedException e) { ; }
    }
  }
}
