import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

/**
 * This class defines the applet, and sets up the Model and View objects.
 * If we had user controls (such as Start and Stop buttons, or a speed
 * control), this is where they would be defined and handled.<p>
 * This example is more complex than it needs to be, because it enforces
 * a strict separation between the model (the part that does the actual
 * computation), the display, and the controls in the user interface (of
 * which there are none). This is called the MVC (Model-View-Controller)
 * pattern, and while it may be overkill for this example, MVC provides an
 * excellent basis for more complex animations.
 */
public class Controller extends Applet {

	// Declare components here, where they are visible to inner classes
	Panel buttonPanel = new Panel ();
	Button runButton = new Button ("  Start  ");

  // Create the model and the view
  Thread animation;
  Model model = new Model();
  View view = new View();

  // Define the images that will be used
  Image graveyard;
  Image skull[] = new Image[25];
  
  /**
   * Lays out the applet components, starts a new thread for the
   * view to do its animation, and gives the model, view, and
   * controller access to one another.
   */
  public void init() {

    // Lay out components
    setLayout(new BorderLayout());
    this.add(BorderLayout.CENTER, view);
    buttonPanel.add (runButton);
    buttonPanel.setBackground(Color.black);
    this.add (BorderLayout.SOUTH, buttonPanel);
    
    // Get applet parameters and put them where they belong
    view.delay = Integer.valueOf(getParameter("delay")).intValue();
    
    // Tell the view about the model and the controller
    view.model = model;
    view.controller = this;
    
    // Get everything set up for the view BEFORE starting its Thread
    loadImages();
    view.alive = true;
    view.okToRun = false;
    
    // Give the view its own thread in which to do the animation
    animation = new Thread(view);
    animation.start();
    
    // Attach actions to components
    runButton.addActionListener (new ActionListener () {
      public void actionPerformed (ActionEvent event) {
        if (view.okToRun) {
          view.okToRun = false;
          showStatus ("Animation is stopped.");
          runButton.setLabel ("  Start  ");
        }
        else {
          view.okToRun = true;
          showStatus ("Animation is running.");
          runButton.setLabel ("  Stop   ");
       }
      }});
  }
  
  /**
   * Finds the size of the canvas (this can't be done in init()
   * because the canvas isn't ready yet) and gives the animation
   * permission to run.
   */
  public void start() {
    Dimension viewSize = view.getSize();
    
    model.skullWidth = 100;
    model.skullHeight = 100;
    model.xLimit = viewSize.width - model.skullWidth;
    model.yLimit = viewSize.height - model.skullHeight;
  }
  
  /**
   * Withdraws permission for the animation to run.
   */
  public void stop() {
    view.okToRun = false;
  }
  
  /**
   * Tells the animation thread to stop running.
   */
  public void destroy() {
    view.alive = false;
  }
  
  /**
   * Loads in the images we will use in this animation. There is an easier
   * way to do this in Java 2, but this method also works with Java 1.1.
   */
   void loadImages() {
     
     // Create a MediaTracker to wait for the images to be loaded
     MediaTracker tracker = new MediaTracker(this);     

     // Declare variables for the URLs
     URL appletLocation = getCodeBase();
     URL graveyardURL = null;
     URL skullURL = null
     ;
     try {
       // Start loading the graveyard image
       graveyardURL = new URL(appletLocation +  "/images/boothill-blue.jpg");
       graveyard = getImage(graveyardURL);
       tracker.addImage(graveyard, 0);
       // Start loading each of the skull images
       for (int i = 0; i < 25; i++) {
         skullURL = new URL(appletLocation +  "/images/t-skull-" + i + ".gif");
         skull[i] = getImage(skullURL);
         tracker.addImage(skull[i], 0);
       }
     }
     catch (MalformedURLException e) { e.printStackTrace(); }
     
     // Wait for the images to be loaded; does no error checking
     try { tracker.waitForAll(5000); }
     catch (InterruptedException e) {}
   }
}

