FileDocCategorySizeDatePackage
Animator.javaAPI DocExample1690Thu Apr 03 15:16:08 BST 1997None

Animator.java

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

public class Animator extends Applet implements Runnable {

  boolean running = false;
  int thisCell = 0;
  Vector cells;
  Thread play;
  MediaTracker theTracker;

  public void init() {

    String nextCell;
    cells = new Vector();
    theTracker = new MediaTracker(this);
    for (int i = 0; (nextCell = getParameter("Cell" + i)) != null ; i++) {
      Image img = getImage(getDocumentBase(), nextCell);
      cells.addElement(img);
      theTracker.addImage(img, i);      
    }
    
    // start loading the images
    theTracker.checkAll(true);
    play = new Thread(this);
    play.start();
    running = true;
  
  }
  
  
  public void run() {
  
    for (thisCell=0; thisCell < cells.size(); thisCell++) {
    
      try {
        // make sure this cell is loaded
        theTracker.waitForID(thisCell);
        // paint the cell
        repaint();
        // sleep for a tenth of a second
        // i.e. play ten frames a second
        play.sleep(100);
      }
      catch (InterruptedException ie) {      
      }
    }
  
  }

  public void stop() {

    play.suspend();

  }
  
  public void start() {

    play.resume();

  }

  public void paint(Graphics g) {
   
    g.drawImage((Image) cells.elementAt(thisCell), 0, 0, this);
    
  }
  
  // The convention is that a mouseClick starts
  // a stopped applet and stops a running applet.
  public boolean mouseUp(Event e, int x, int y) {
  
    if (running) {
      play.suspend();
      running = false;
    }
    else {
      play.resume();
      running = true;
    }
    
    return true;  
          
  }

}