// WildAnimals Class
// WildAnimals.java

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

public class WildAnimals extends Applet implements Runnable {
  private Image         offImage;
  private AudioClip[]   clip = new AudioClip[5];
  private Graphics      offGrfx;
  private Thread        animate;
  private MediaTracker  tracker;
  private SpriteVector  sv;
  private int           delay = 83; // 12 fps
  private Random        rand = new
    Random(System.currentTimeMillis());

  public String getAppletInfo() {
    return (new String("Wild Animals by Michael Morrison"));
  }

  public void init() {
    // Load and track the images
    tracker = new MediaTracker(this);
    Eyes.initResources(this, tracker, 0);

    // Load the audio clips
    clip[0] = getAudioClip(getCodeBase(), "Res/Crow.au");
    clip[1] = getAudioClip(getCodeBase(), "Res/Hyena.au");
    clip[2] = getAudioClip(getCodeBase(), "Res/Monkey.au");
    clip[3] = getAudioClip(getCodeBase(), "Res/Tiger.au");
    clip[4] = getAudioClip(getCodeBase(), "Res/Wolf.au");
  }

  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 the sprites
    sv = new SpriteVector(new ColorBackground(this, Color.black));
    for (int i = 0; i < 8; i++) {
      sv.add(new Eyes(this, new Point(Math.abs(rand.nextInt() %
        size().width), Math.abs(rand.nextInt() % size().width)),
        i % 2, Math.abs(rand.nextInt() % 200)));
    }

    // Update everything
    long t = System.currentTimeMillis();
    while (Thread.currentThread() == animate) {
      // Update the animations
      sv.update();
      repaint();

      // Play an animal sound
      if ((rand.nextInt() % 15) == 0)
        clip[Math.abs(rand.nextInt() % 5)].play();

      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
    sv.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("Wild Animals"),
                  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());
    }
  }
}
