/*
   Source code by David Eck
                  Department of Mathematics and Computer Science
                  Hobart and William Smith Colleges
                  Geneva, NY 14456
                  eck@hws.edu
                  
   This Java source code file can be used IN UNMODIFIED FORM for
   any purpose.  You can also make and distribute modified versions,
   as long as you include an acknowledgement of the original
   author in the modified version.
*/

/*
   <applet code="EdgeOfChaosApplet.class" width=600 height=500>
      <param name="Example1" value="ca_params_first.txt">
      <param name="Example2" value="ca_params_second.txt">
      <param name="Example3" value="bogus.txt">
   </applet>
*/


import xca.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.Vector;

public class EdgeOfChaosApplet extends JApplet {

   private WorldPane worldPane;
   private boolean running;
   private boolean created;
   private Menus menus;

   public EdgeOfChaosApplet() {
      getContentPane().setBackground(Color.gray);
      getRootPane().setBorder(BorderFactory.createLineBorder(Color.black));
      MainPanel p = new MainPanel(false);
      getContentPane().add(p);
      menus = new Menus(p.getWorldPane(),false,null);
      setJMenuBar(menus);
      worldPane = p.getWorldPane();
   }
   
   public void start() {
      if (!created) {
         String[] fileList = getFileParams();
         if (fileList != null && fileList.length > 0)
            menus.addExamplesMenu(fileList,(java.applet.Applet)this);
         World world = new World(5,4,worldPane.getWidth(),true,true);
         worldPane.installWorld(world,worldPane.getHeight());
         worldPane.start(500);
         created = true;
         running = true;
      }
      else if (running)
         worldPane.start(100);
   }
   
   public void stop() {
      running = worldPane.isRunning();
      worldPane.stop();
   }
   
   private void create() {
      World world = new World(5,4,getWidth()-4,true,true);
   }
   
   private String[] getFileParams() {
      Vector v = new Vector();
      String s = getParameter("Example");
      if (s != null)
         v.addElement(s);
      s = getParameter("Example0");
      if (s != null)
         v.addElement(s);
      int ct = 1;
      do {
         s = getParameter("Example" + ct);
         ct++;
         if (s != null)
            v.addElement(s);
      } while (s != null);
      if (v.size() == 0)
         return null;
      else {
         String[] files = new String[v.size()];
         for (int i = 0; i < v.size(); i++)
            files[i] = (String)v.elementAt(i);
         return files;
      }
   }

}
