import java.applet.*;
import java.awt.*;
import Raster;
import Sprite;
import AnimatedSprite;
import Playfield;

public class Project1 extends Applet implements Runnable {
  Playfield screen;
  Image output;
  Thread animation_thread;
  int tick_counter = 0;

  public void init() {
    AnimatedSprite as;

    /* Initialize the background and sprites of the playfield. */
    screen = new Playfield (getImage(getDocumentBase(), "simpcity.gif"), 6);
    as = new AnimatedSprite (getImage(getDocumentBase(), "mil_scan2.gif"), 1);
    as.setStateMovement (0, 3, 3);
    screen.addSprite (as);
    as = new AnimatedSprite (getImage(getDocumentBase(), "strdduch.gif"), 1);
    as.setStateMovement (0, -2, -2);
    as.setCoordinates (200, 200);
    screen.addSprite (as);
    as = new AnimatedSprite (getImage(getDocumentBase(), "streborg.gif"), 1);
    as.setStateMovement (0, -5, 3);
    as.setCoordinates (300, 0);
    screen.addSprite (as);
    as = new AnimatedSprite (getImage(getDocumentBase(), "strbmidl.gif"), 1);
    as.setStateMovement (0, 6, -5);
    as.setCoordinates (0, 200);
    screen.addSprite (as);
    as = new AnimatedSprite (getImage(getDocumentBase(), "straero1.gif"), 1);
    as.setStateMovement (0, 1, 4);
    as.setCoordinates (300, 0);
    screen.addSprite (as);
    as = new AnimatedSprite (getImage(getDocumentBase(), "margeconfused.gif"), 1);
    as.setStateMovement (0, 7, -2);
    as.setCoordinates (150, 0);
    screen.addSprite (as);
    createImage ();
  }

  public void start () {
    animation_thread = new Thread (this);
    animation_thread.start ();
  }

  public void stop () {
    animation_thread = null;
  }

  public void run () {

    /* Simply start ticking away for 1000 ticks. */
    while (tick_counter < 1000) {
      /* We need to sleep so we don't animate too fast. */
      try {
	animation_thread.sleep (100);
      } catch (InterruptedException e) {
      }
      createImage ();
    }
  }

  public void createImage () {
    output = screen.toRaster().toImage (this);
    screen.nextTick ();
    tick_counter++;
    repaint ();
  }

  public void paint(Graphics g) {
    g.drawImage (output, 0, 0, this);
  }
  
  public void update(Graphics g) {
    paint (g);
  }
  
  public boolean mouseUp (Event e, int x, int y) {         // not Java 1.1
    /* We simply pass a mouse up event to the playfield,
     * which has its own mouseUp handler. 
     */
    screen.mouseUp (x, y);
    createImage ();
    return true;
  }
}
