//file: TrackImage.java
import java.awt.*;

public class TrackImage extends javax.swing.JApplet
                        implements Runnable {
  final int MAIN_IMAGE = 0;
  Image image;
  MediaTracker tracker;
  boolean loaded = false;
  Thread thread = null;
  String message = "Loading...";

  public void init(  ) {
    image = getImage(getClass(  ).getResource(getParameter("image")));
    tracker = new MediaTracker(this);
    tracker.addImage(image, MAIN_IMAGE);
  }

  public void start(  ) {
    if (!tracker.checkID(MAIN_IMAGE)) {
      thread = new Thread(this);
      thread.start(  );
    }
  }

  public void stop(  ) {
    thread.interrupt(  );
    thread = null;
  }

  public void run(  ) {
    repaint(  );
    try { tracker.waitForID(MAIN_IMAGE); }
    catch(InterruptedException e) {}
    if (tracker.isErrorID(MAIN_IMAGE)) message = "Error";
    else loaded = true;
    repaint(  );
  }

  public void paint(Graphics g) {
    if (loaded) g.drawImage(image, 0, 0, this);
    else {
      g.drawRect(0, 0, getSize().width - 1, getSize(  ).height - 1);
      g.drawString(message, 20, 20);
    }
  }
}
