Game-of-Life-WSU-CEG4180  v0.1
RefactoringtheGameofLife
GameOfLife.java
Go to the documentation of this file.
1 /* This page is part of the Game of Life source code */
2 
3 /**
4  * Game of Life v1.4
5  * Copyright 1996-2004 Edwin Martin <edwin@bitstorm.nl>
6  * version 1.0 online since July 3 1996
7  * Changes:
8  * 1.1: Double buffering to screen; faster paint
9  * 1.2: Arrowkeys changed; better use of `synchronized'
10  * 1.3: Choose speed from drop down menu and draw with mouse
11  * 1.4: Use Java 1.1 events, remove 13 deprecated methods, some refactoring. 2003-11-08
12  * 1.5: Lots of refactoring, zooming, small improvements
13  * @author Edwin Martin
14  *
15  */
16 
17 package org.bitstorm.gameoflife;
18 
19 import java.applet.Applet;
20 import java.awt.Color;
21 import java.awt.GridBagConstraints;
22 import java.awt.GridBagLayout;
23 
24 /**
25  * The Game Of Life Applet.
26  * This is the heart of the program. It initializes everything en put it together.
27  * @author Edwin Martin
28  */
29 public class GameOfLife extends Applet implements Runnable, GameOfLifeControlsListener {
32  protected int cellSize;
33  protected int cellCols;
34  protected int cellRows;
35  protected int genTime;
37  protected static Thread gameThread = null;
38 
39  /**
40  * Initialize UI.
41  * @see java.applet.Applet#init()
42  */
43  public void init() {
44  getParams();
45 
46  // set background colour
47  setBackground(new Color(0x999999));
48 
49  // create gameOfLifeGrid
50  gameOfLifeGrid = new GameOfLifeGrid(cellCols, cellRows);
51  gameOfLifeGrid.clear();
52 
53  // create GameOfLifeCanvas
54  gameOfLifeCanvas = new CellGridCanvas(gameOfLifeGrid, cellSize);
55 
56  // create GameOfLifeControls
57  controls = new GameOfLifeControls();
58  controls.addGameOfLifeControlsListener( this );
59 
60  // put it all together
61  GridBagLayout gridbag = new GridBagLayout();
62  setLayout(gridbag);
63  GridBagConstraints canvasContraints = new GridBagConstraints();
64 
65  canvasContraints.fill = GridBagConstraints.BOTH;
66  canvasContraints.gridx = GridBagConstraints.REMAINDER;
67  canvasContraints.gridy = 0;
68  canvasContraints.weightx = 1;
69  canvasContraints.weighty = 1;
70  canvasContraints.anchor = GridBagConstraints.CENTER;
71  gridbag.setConstraints(gameOfLifeCanvas, canvasContraints);
72  add(gameOfLifeCanvas);
73 
74  GridBagConstraints controlsContraints = new GridBagConstraints();
75  canvasContraints.gridy = 1;
76  canvasContraints.gridx = 0;
77  controlsContraints.gridx = GridBagConstraints.REMAINDER;
78  gridbag.setConstraints(controls, controlsContraints);
79  add(controls);
80 
81  try {
82  // Start with a shape (My girlfriend clicked "Start" on a blank screen and wondered why nothing happened).
84  } catch (ShapeException e) {
85  // Ignore. Not going to happen.
86  }
87  setVisible(true);
88  validate();
89  }
90 
91  /**
92  * Get params (cellSize, cellCols, cellRows, genTime) from applet-tag
93  */
94  protected void getParams() {
95  cellSize = getParamInteger( "cellsize", 11 );
96  cellCols = getParamInteger( "cellcols", 50 );
97  cellRows = getParamInteger( "cellrows", 30 );
98  genTime = getParamInteger( "gentime", 1000 );
99  }
100 
101  /**
102  * Read applet parameter (int) or, when unavailable, get default value.
103  * @param name name of parameter
104  * @param defaultParam default when parameter is unavailable
105  * @return value of parameter
106  */
107  protected int getParamInteger( String name, int defaultParam ) {
108  String param;
109  int paramInt;
110 
111  param = getParameter( name );
112  if ( param == null )
113  paramInt = defaultParam;
114  else
115  paramInt = Integer.valueOf(param).intValue();
116  return paramInt;
117  }
118 
119  /**
120  * Starts creating new generations.
121  * No start() to prevent starting immediately.
122  */
123  public synchronized void start2() {
124  controls.start();
125  if (gameThread == null) {
126  gameThread = new Thread(this);
127  gameThread.start();
128  }
129  }
130 
131  /**
132  * @see java.applet.Applet#stop()
133  */
134  public void stop() {
135  controls.stop();
136  gameThread = null;
137  }
138 
139  /**
140  * @see java.lang.Runnable#run()
141  */
142  public synchronized void run() {
143  while (gameThread != null) {
144  nextGeneration();
145  try {
146  Thread.sleep(genTime);
147  } catch (InterruptedException e) {
148  e.printStackTrace();
149  }
150  }
151  }
152 
153  /**
154  * Is the applet running?
155  * @return true: applet is running
156  */
157  public boolean isRunning() {
158  return gameThread != null;
159  }
160 
161  /**
162  * Go to the next generation.
163  */
164  public void nextGeneration() {
165  gameOfLifeGrid.next();
166  gameOfLifeCanvas.repaint();
167  showGenerations();
168  }
169 
170  /**
171  * Set the new shape
172  * @param shape name of shape
173  */
174  public void setShape( Shape shape ) {
175  if ( shape == null )
176  return;
177 
178  try {
179  gameOfLifeCanvas.setShape( shape );
180  reset();
181  } catch (ShapeException e) {
182  alert( e.getMessage() );
183  }
184  }
185 
186  /**
187  * Resets applet (after loading new shape)
188  */
189  public void reset() {
190  stop(); // might otherwise confuse user
191  gameOfLifeCanvas.repaint();
192  showGenerations();
193  showStatus( "" );
194  }
195 
196  /**
197  * @see java.applet.Applet#getAppletInfo()
198  */
199  public String getAppletInfo() {
200  return "Game Of Life v. 1.5\nCopyright 1996-2004 Edwin Martin";
201  }
202 
203  /**
204  * Show number of generations.
205  */
206  private void showGenerations() {
207  controls.setGeneration( gameOfLifeGrid.getGenerations() );
208  }
209 
210  /**
211  * Set speed of new generations.
212  * @param fps generations per second
213  */
214  public void setSpeed( int fps ) {
215  genTime = fps;
216  }
217 
218  /**
219  * Sets cell size.
220  * @param p size of cell in pixels
221  */
222  public void setCellSize( int p ) {
223  cellSize = p;
224  gameOfLifeCanvas.setCellSize( cellSize );
225  }
226 
227  /**
228  * Gets cell size.
229  * @return size of cell
230  */
231  public int getCellSize() {
232  return cellSize;
233  }
234 
235  /**
236  * Shows an alert
237  * @param s text to show
238  */
239  public void alert( String s ) {
240  showStatus( s );
241  }
242 
243  /** Callback from GameOfLifeControlsListener
244  * @see org.bitstorm.gameoflife.GameOfLifeControlsListener#startStopButtonClicked(org.bitstorm.gameoflife.GameOfLifeControlsEvent)
245  */
247  if ( isRunning() ) {
248  stop();
249  } else {
250  start2();
251  }
252  }
253 
254  /** Callback from GameOfLifeControlsListener
255  * @see org.bitstorm.gameoflife.GameOfLifeControlsListener#nextButtonClicked(org.bitstorm.gameoflife.GameOfLifeControlsEvent)
256  */
258  nextGeneration();
259  }
260 
261  /** Callback from GameOfLifeControlsListener
262  * @see org.bitstorm.gameoflife.GameOfLifeControlsListener#speedChanged(org.bitstorm.gameoflife.GameOfLifeControlsEvent)
263  */
265  setSpeed( e.getSpeed() );
266  }
267 
268  /** Callback from GameOfLifeControlsListener
269  * @see org.bitstorm.gameoflife.GameOfLifeControlsListener#speedChanged(org.bitstorm.gameoflife.GameOfLifeControlsEvent)
270  */
272  setCellSize( e.getZoom() );
273  }
274 
275  /** Callback from GameOfLifeControlsListener
276  * @see org.bitstorm.gameoflife.GameOfLifeControlsListener#shapeSelected(org.bitstorm.gameoflife.GameOfLifeControlsEvent)
277  */
279  String shapeName = (String) e.getShapeName();
280  Shape shape;
281  try {
282  shape = ShapeCollection.getShapeByName( shapeName );
283  setShape( shape );
284  } catch (ShapeException e1) {
285  // Ignore. Not going to happen.
286  }
287  }
288 }
int getParamInteger(String name, int defaultParam)
void speedChanged(GameOfLifeControlsEvent e)
synchronized void setShape(Shape shape)
void shapeSelected(GameOfLifeControlsEvent e)
static Shape getShapeByName(String name)
void nextButtonClicked(GameOfLifeControlsEvent e)
void zoomChanged(GameOfLifeControlsEvent e)
void addGameOfLifeControlsListener(GameOfLifeControlsListener listener)
void startStopButtonClicked(GameOfLifeControlsEvent e)