//****************************************************************************** // CellLife.java: Applet // //****************************************************************************** import java.applet.*; import java.awt.*; import java.util.*; import tjhCell; import tjhCreature; import tjhBuckets; //============================================================================== // Main Class for applet CellLife // //============================================================================== /** Our main applet class. Draw the creatures and manages their lives. Stores a list of creatures and a list of references to all the cells in the universe, to simplify collision detection. Keeps track of how much time has passed. */ public class CellLife extends Applet implements Runnable { //---------------------------------------------------------------------- // data //---------------------------------------------------------------------- /** m_CellLife is the Thread object for the applet */ private Thread m_CellLife = null; //---------------------------------------------------------------------- // static constants //---------------------------------------------------------------------- /** the maximum number of creatures in the world. When the number of creatures alive drops below half this, a new one is created to bring the numbers back up. */ protected static final int MAX_CREATURES=60; //---------------------------------------------------------------------- // data //---------------------------------------------------------------------- /** A list of the creatures currently alive. Stores tjhCreature references. */ protected Vector creatures; /** The world is a rectangle from (0,0) to (limit.x,limit,y) */ protected tjh2dVector limit; /** The number of creatures that have been born since the simulation started */ protected long generations; /** A test creature controllable by the user to allow response testing */ private tjhCreature test_creature; /** space-partitioning structure to speed collision detection */ protected tjhBuckets buckets; //---------------------------------------------------------------------- // methods //---------------------------------------------------------------------- /** CellLife Class Constructor */ public CellLife() { creatures = new Vector(); limit = new tjh2dVector(500.0F,500.0F); generations = 0; // initialize our buckets structure float bucket_scale = tjhCell.RADIUS; // could stretch to root-two times this and still enforce one cell per bucket buckets = new tjhBuckets(bucket_scale,(int)Math.ceil(limit.x/bucket_scale),(int)Math.ceil(limit.y/bucket_scale)); } // APPLET INFO SUPPORT: /** The getAppletInfo() method returns a string describing the applet's author, copyright date, or miscellaneous information.*/ //-------------------------------------------------------------------------- public String getAppletInfo() { return "Name: CellLife\r\n" + "Author: Tim Hutton\r\n" + "Compiled with Sun's JDK"; } // The init() method is called by the AWT when an applet is first loaded or // reloaded. Override this method to perform whatever initialization your // applet needs, such as initializing data structures, loading images or // fonts, creating frame windows, setting the layout manager, or adding UI // components. //-------------------------------------------------------------------------- /** first-time initialization */ public void init() { // If you use a ResourceWizard-generated "control creator" class to // arrange controls in your applet, you may want to call its // CreateControls() method from within this method. Remove the following // call to resize() before adding the call to CreateControls(); // CreateControls() does its own resizing. //---------------------------------------------------------------------- resize((int)limit.x,(int)limit.y); for(int i=0;i