Game-of-Life-WSU-CEG4180  v0.1
RefactoringtheGameofLife
GameOfLifeControls.java
Go to the documentation of this file.
1 /* This page is part of the Game of Life source code */
2 
3 /**
4  * The control bar at the bottom.
5  * Is put in a seperate object, so it can be replaced by another UI, e.g. on a J2ME phone.
6  * Copyright 1996-2004 Edwin Martin <edwin@bitstorm.nl>
7  * @author Edwin Martin
8  */
9 
10 package org.bitstorm.gameoflife;
11 
12 import java.awt.Button;
13 import java.awt.Choice;
14 import java.awt.Label;
15 import java.awt.Panel;
16 import java.awt.event.ActionEvent;
17 import java.awt.event.ActionListener;
18 import java.awt.event.ItemEvent;
19 import java.awt.event.ItemListener;
20 import java.util.Enumeration;
21 import java.util.Vector;
22 
23 
24 /**
25  * GUI-controls of the Game of Life.
26  * It contains controls like Shape, zoom and speed selector, next and start/stop-button.
27  * It is a seperate class, so it can be replaced by another implementation for e.g. mobile phones or PDA's.
28  * Communicates via the GameOfLifeControlsListener.
29  * @author Edwin Martin
30  *
31  */
32 public class GameOfLifeControls extends Panel {
33  private Label genLabel;
34  private final String genLabelText = "Generations: ";
35  private final String nextLabelText = "Next";
36  private final String startLabelText = "Start";
37  private final String stopLabelText = "Stop";
38  public static final String SLOW = "Slow";
39  public static final String FAST = "Fast";
40  public static final String HYPER = "Hyper";
41  public static final String BIG = "Big";
42  public static final String MEDIUM = "Medium";
43  public static final String SMALL = "Small";
44  public static final int SIZE_BIG = 11;
45  public static final int SIZE_MEDIUM = 7;
46  public static final int SIZE_SMALL = 3;
47  private Button startstopButton;
48  private Button nextButton;
49  private Vector listeners;
50  private Choice shapesChoice;
51  private Choice zoomChoice;
52 
53  /**
54  * Contructs the controls.
55  */
56  public GameOfLifeControls() {
57  listeners = new Vector();
58 
59  // pulldown menu with shapes
60  shapesChoice = new Choice();
61 
62  // Put names of shapes in menu
63  Shape[] shapes = ShapeCollection.getShapes();
64  for ( int i = 0; i < shapes.length; i++ )
65  shapesChoice.addItem( shapes[i].getName() );
66 
67  // when shape is selected
68  shapesChoice.addItemListener(
69  new ItemListener() {
70  public void itemStateChanged(ItemEvent e) {
71  shapeSelected( (String) e.getItem() );
72  }
73  }
74  );
75 
76  // pulldown menu with speeds
77  Choice speedChoice = new Choice();
78 
79  // add speeds
80  speedChoice.addItem(SLOW);
81  speedChoice.addItem(FAST);
82  speedChoice.addItem(HYPER);
83 
84  // when item is selected
85  speedChoice.addItemListener(
86  new ItemListener() {
87  public void itemStateChanged(ItemEvent e) {
88  String arg = (String) e.getItem();
89  if (SLOW.equals(arg)) // slow
90  speedChanged(1000);
91  else if (FAST.equals(arg)) // fast
92  speedChanged(100);
93  else if (HYPER.equals(arg)) // hyperspeed
94  speedChanged(10);
95  }
96  }
97  );
98 
99  // pulldown menu with speeds
100  zoomChoice = new Choice();
101 
102  // add speeds
103  zoomChoice.addItem(BIG);
104  zoomChoice.addItem(MEDIUM);
105  zoomChoice.addItem(SMALL);
106 
107  // when item is selected
108  zoomChoice.addItemListener(
109  new ItemListener() {
110  public void itemStateChanged(ItemEvent e) {
111  String arg = (String) e.getItem();
112  if (BIG.equals(arg))
113  zoomChanged(SIZE_BIG);
114  else if (MEDIUM.equals(arg))
115  zoomChanged(SIZE_MEDIUM);
116  else if (SMALL.equals(arg))
117  zoomChanged(SIZE_SMALL);
118  }
119  }
120  );
121 
122  // number of generations
123  genLabel = new Label(genLabelText+" ");
124 
125  // start and stop buttom
126  startstopButton = new Button(startLabelText);
127 
128  // when start/stop button is clicked
129  startstopButton.addActionListener(
130  new ActionListener() {
131  public void actionPerformed(ActionEvent e) {
133  }
134  }
135  );
136 
137  // next generation button
138  nextButton = new Button(nextLabelText);
139 
140  // when next button is clicked
141  nextButton.addActionListener(
142  new ActionListener() {
143  public void actionPerformed(ActionEvent e) {
145  }
146  }
147  );
148 
149  // create panel with controls
150  this.add(shapesChoice);
151  this.add(nextButton);
152  this.add(startstopButton);
153  this.add(speedChoice);
154  this.add(zoomChoice);
155  this.add(genLabel);
156  this.validate();
157  }
158 
159 
160  /**
161  * Add listener for this control
162  * @param listener Listener object
163  */
165  listeners.addElement( listener );
166  }
167 
168  /**
169  * Remove listener from this control
170  * @param listener Listener object
171  */
173  listeners.removeElement( listener );
174  }
175 
176  /**
177  * Set the number of generations in the control bar.
178  * @param generations number of generations
179  */
180  public void setGeneration( int generations ) {
181  genLabel.setText(genLabelText + generations + " ");
182  }
183 
184  /**
185  * Start-button is activated.
186  */
187  public void start() {
188  startstopButton.setLabel(stopLabelText);
189  nextButton.disable();
190  shapesChoice.disable();
191  }
192 
193  /**
194  * Stop-button is activated.
195  */
196  public void stop() {
197  startstopButton.setLabel(startLabelText);
198  nextButton.enable();
199  shapesChoice.enable();
200  }
201 
202  /**
203  * Called when the start/stop-button is clicked.
204  * Notify event-listeners.
205  */
206  public void startStopButtonClicked() {
208  for ( Enumeration e = listeners.elements(); e.hasMoreElements(); )
209  ((GameOfLifeControlsListener) e.nextElement()).startStopButtonClicked( event );
210  }
211 
212  /**
213  * Called when the next-button is clicked.
214  * Notify event-listeners.
215  */
216  public void nextButtonClicked() {
218  for ( Enumeration e = listeners.elements(); e.hasMoreElements(); )
219  ((GameOfLifeControlsListener) e.nextElement()).nextButtonClicked( event );
220  }
221 
222  /**
223  * Called when a new speed from the speed pull down is selected.
224  * Notify event-listeners.
225  */
226  public void speedChanged( int speed ) {
228  for ( Enumeration e = listeners.elements(); e.hasMoreElements(); )
229  ((GameOfLifeControlsListener) e.nextElement()).speedChanged( event );
230  }
231 
232  /**
233  * Called when a new zoom from the zoom pull down is selected.
234  * Notify event-listeners.
235  */
236  public void zoomChanged( int zoom ) {
238  for ( Enumeration e = listeners.elements(); e.hasMoreElements(); )
239  ((GameOfLifeControlsListener) e.nextElement()).zoomChanged( event );
240  }
241 
242  /**
243  * Called when a new shape from the shape pull down is selected.
244  * Notify event-listeners.
245  */
246  public void shapeSelected( String shapeName ) {
248  for ( Enumeration e = listeners.elements(); e.hasMoreElements(); )
249  ((GameOfLifeControlsListener) e.nextElement()).shapeSelected( event );
250  }
251 
252  /**
253  * Called when a new cell size from the zoom pull down is selected.
254  * Notify event-listeners.
255  */
256  public void setZoom( String n ) {
257  zoomChoice.select(n);
258  }
259 
260 
261 }
void removeGameOfLifeControlsListener(GameOfLifeControlsListener listener)
static GameOfLifeControlsEvent getShapeSelectedEvent(Object source, String shapeName)
static GameOfLifeControlsEvent getSpeedChangedEvent(Object source, int speed)
static GameOfLifeControlsEvent getZoomChangedEvent(Object source, int zoom)
void addGameOfLifeControlsListener(GameOfLifeControlsListener listener)