// SimTarantula Class
// SimTarantula.java

// Imports
import java.awt.*;
import java.applet.Applet;
import java.util.Random;

public class SimTarantula extends Applet implements Runnable {
  private Image           offImage, back;
  private Graphics        offGrfx;
  private Thread          animate;
  private MediaTracker    tracker;
  private TarantulaVector tv;
  private int             delay = 83; // 12 fps
  private Random          rand = new
    Random(System.currentTimeMillis());

  public String getAppletInfo() {
    return (new String("Sim Tarantula by Michael Morrison"));
  }

  public void init() {
    // Load and track the images
    tracker = new MediaTracker(this);
    back = getImage(getCodeBase(), "Res/Back.gif");
    tracker.addImage(back, 0);
    Tarantula.initResources(this, tracker, 0);
    Spiderling.initResources(this, tracker, 0);
    Spidercide.initResources(this, tracker, 0);
  }

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

  public void stop() {
    if (animate != null) {
      animate.stop();
      animate = null;
    }
  }

  public void run() {
    try {
      tracker.waitForID(0);
    }
    catch (InterruptedException e) {
      return;
    }

    // Create and add some spiderlings
    tv = new TarantulaVector(new ImageBackground(this, back));
    for (int i = 0; i < 5; i++) {
      Point pos = tv.getEmptyPosition(new Dimension(
        Spiderling.image[0].getWidth(this),
        Spiderling.image[0].getHeight(this)));
      tv.add(new Spiderling(this, pos));
    }

    // Update everything
    long t = System.currentTimeMillis();
    while (Thread.currentThread() == animate) {
      tv.update();
      repaint();
      try {
        t += delay;
        Thread.sleep(Math.max(0, t - System.currentTimeMillis()));
      }
      catch (InterruptedException e) {
        break;
      }
    }
  }

  public void update(Graphics g) {
    // Create the offscreen graphics context
    if (offGrfx == null) {
      offImage = createImage(size().width, size().height);
      offGrfx = offImage.getGraphics();
    }

    // Draw the sprites
    tv.draw(offGrfx);

    // Draw the image onto the screen
    g.drawImage(offImage, 0, 0, null);
  }

  public void paint(Graphics g) {
    if ((tracker.statusID(0, true) & MediaTracker.ERRORED) != 0) {
      // Draw the error rectangle
      g.setColor(Color.red);
      g.fillRect(0, 0, size().width, size().height);
      return;
    }
    if ((tracker.statusID(0, true) & MediaTracker.COMPLETE) != 0) {
      // Draw the offscreen image
      g.drawImage(offImage, 0, 0, null);
    }
    else {
      // Draw the title message (while the images load)
      Font        f1 = new Font("TimesRoman", Font.BOLD, 28),
                  f2 = new Font("Helvetica", Font.PLAIN, 16);
      FontMetrics fm1 = g.getFontMetrics(f1),
                  fm2 = g.getFontMetrics(f2);
      String      s1 = new String("Sim Tarantula"),
                  s2 = new String("Loading images...");
      g.setFont(f1);
      g.drawString(s1, (size().width - fm1.stringWidth(s1)) / 2,
        ((size().height - fm1.getHeight()) / 2) + fm1.getAscent());
      g.setFont(f2);
      g.drawString(s2, (size().width - fm2.stringWidth(s2)) / 2,
        size().height - fm2.getHeight() - fm2.getAscent());
    }
  }
}
