import java.awt.*;
import java.net.URL;
import java.applet.Applet;

/**
 * MorphPlayer
 *
 * an applet that just plays a morph.
 * Reads in html generated by Saving from the editorapp
 */
public class MorphPlayer extends Applet {
     Morph morph;
     boolean running = true;

     public void init() {
          try {
               int numframes = Integer.parseInt(getParameter("numframes"));
               long wait = Integer.parseInt(getParameter("wait"));
               Grid grid1 = new Grid(getParameter("grid1"));
               Grid grid2 = new Grid(getParameter("grid2"));
               Image img1 = getImage(new URL(getDocumentBase(),
                                   getParameter("img1")));
               Image img2 = getImage(new URL(getDocumentBase(),
                                   getParameter("img2")));
               MediaTracker mt = new MediaTracker(this);
               mt.addImage(img1, 0);
               mt.addImage(img2, 0);
               mt.waitForAll();

               morph = new Morph(img1, img2, numframes);
               morph.setPause(wait);
               morph.newGrids(grid1, grid2);
               setLayout(new BorderLayout());
               add("Center", morph);
          } catch (Exception e) {
               System.err.println("Error parsing parameters");
               morph = null;
          }
     }

     public void start() {
          if (morph != null) {
               morph.start();
          }
     }

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

     public boolean mouseUp(Event evt, int x, int y) {
          if (running) {
               stop();
          } else {
               start();
          }
          running = !running;
          return true;
     }
}
