/* An applet to implement J.H.C. Conway's Game of Life */ /* We work on a rectangular grid with toroidal boundary conditions */ import java.awt.*; public class LifeApplet extends java.applet.Applet implements Runnable { final int HORPIXELS = 640; // Edit the html if you change these final int VERTPIXELS = 480; final int CELLSIZE = 10; // size of side of cells in pixels final int DEFAULT_SLEEP_INTERVAL = 10; // millisecs to wait between generations // states of the program final int SETUP = 0; // setting up initial population final int RUNNING = 1; // letting population evolve final int SUSPENDED = 2; int rows,cols; int NumberOfSpots; int time; int state; int sleep_time = DEFAULT_SLEEP_INTERVAL; int CurrentState[][]; // The current and previous population arrays int LastState[][]; boolean grid; // whether or not a grid has been fully drawn boolean clear; boolean force_redraw; Thread myThread; Panel MenuPanel; Label sizeReadout; Label generationReadout; MyCanvas PlayingField; // class MyCanvas is defined below. Place where the // populations are drawn. public void init() { // set up the on screen layout of the app. This is a border layout // with a control panel having several buttons and readouts across // the top, and a canvas below where each successive generation is // drawn. setLayout( new BorderLayout()); PlayingField = new MyCanvas(this); MenuPanel = new Panel(); add("North",MenuPanel); add("Center",PlayingField); MenuPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); MenuPanel.setBackground(Color.magenta); MenuPanel.add(new Button("Start")); MenuPanel.add(new Button("Pause")); MenuPanel.add(new Button("Reset")); MenuPanel.add(new Label("Size: ")); sizeReadout = new Label("0 ",Label.LEFT); MenuPanel.add((Label)sizeReadout); MenuPanel.add(new Label("Time: ")); generationReadout = new Label("1 ",Label.LEFT); MenuPanel.add((Label)generationReadout); PlayingField.setBackground(Color.blue); CurrentState = new int[HORPIXELS/CELLSIZE][VERTPIXELS/CELLSIZE]; LastState = new int[HORPIXELS/CELLSIZE][VERTPIXELS/CELLSIZE]; cols = HORPIXELS/CELLSIZE; rows = VERTPIXELS/CELLSIZE; } public void start() { // Initialize state of the app for(int j=0;j0) return i-1; else return rows-1; } public int South(int i) { if(i0) return i-1; else return cols-1; } public int East(int i) { if(i