forked from extra-programming/life
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLifeGame.java
More file actions
executable file
·318 lines (260 loc) · 10.9 KB
/
Copy pathLifeGame.java
File metadata and controls
executable file
·318 lines (260 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// file LifeBoard.java
// For some reason bbedit file needs "Western (Mac OS Roman)" with "Mac (CR)" to compile with javac. (Maybe have to save as new file with utf16?)
// Good: "Western (Mac OS Roman)" with "Mac (CR)"
// Bad: UTF-8 with Unicode linebreak
// Bad: UTF-16 with unicode linebreak
// Bad: UTF-8 with unix LF
// Bad: UTF-8 with Mac CR
// package <default>
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.applet.*;
/**
* @author Mike Roam
*/
public class LifeGame extends JApplet /* was Applet */ {
private static final long serialVersionUID = 42L; // makes serializable happy
static JFrame mikeFrame = null; // gui window for standalone, containing everything
/* static */ Container myContentPane = null; // for applets and standalone
public static final int FRAMEWIDTH = 500;
public static final int FRAMEHEIGHT = 500;
LifeBoard theBoard = null;
/* uh-oh: this is JPanel within my borderLayout,
* but when I want to replace it with a new one,
* I'm not sure whether to get rid of the old before adding the new? */
JButton getFileBtn = new JButton( "Get 'life' File..." );
//Button getFileBtn = new Button( "Get 'life' File..." );
JButton randomBoardBtn = new JButton( "Random Board" );
//Button randomBoardBtn = new Button( "Random Board" );
JButton stepOneBtn = new JButton( "Step" );
//Button goMoreBtn = new Button( "Step" );
JButton stepTenBtn = new JButton( "Step 10" );
JButton stepContinuouslyBtn = new JButton( "Run" );
JButton quitBtn = new JButton( "Quit" );
//Button quitBtn = new Button( "Quit" );
JSlider delaySlider = null; /* is instantiated in buildDelayPanel( ) */
Thread stepperThread = new Thread(){
public void run() {
while(true) {
if ( stepContinuouslyBtn.getText().equals("Pause") ) {
lifeStep(1);
}
try {
sleep( delaySlider.getValue() );
} catch( Exception e ) {
e.printStackTrace( System.err );
} /* end of try */
} /* end of while */
} /* run( ) */
}; /* instance stepperThread */
/**
* default constructor, called by main
*/
public LifeGame( ) {
myContentPane = this.getContentPane( );
init( );
} // end of default constructor
public static void main(String args[]) {
LifeGame myLifeGame = new LifeGame( );
JFrame frame = new JFrame(" Life Game ");
frame.getContentPane().add( myLifeGame );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
/* myLifeGame.addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
*/
frame.setSize( FRAMEWIDTH, FRAMEHEIGHT );
// myLifeGame.init( ); the constructor calls init!
myLifeGame.start( ); // what's this??
frame.setVisible(true);
} // main( )
/**
* For starting up Applet
* (Also called by main and constructor so standalone acts like applet)
*/
public void init( ) {
// myContentPane.add( "Center", this ); // necessary?
if (myContentPane == null) {
// // so I guess we're an applet
myContentPane = getContentPane( );
}
/* Start life with a random board */
theBoard = new LifeBoard(/*cellsAcross:*/ 20, /*cellsDown*/20 );
this.buildGUI( );
} // init( )
public boolean validBoardCoordinates(int x,int y){
if(x>=boardMinX&&x<boardMaxX&&y>boardMinY&&y<boardMaxY) return true;
return false;
}
public int getBoardX(int x){
}
public int getBoardY(int y){
}
public void changeCellVal(int x, int y){
}
/**
* applets and standalones call constructor which calls init which calls this.
*/
public void buildGUI() {
// this.setLayout( new BorderLayout( ) ); / /JApplet have this by default
JPanel setupPanel = new JPanel();
setupPanel.setBorder(BorderFactory.createEmptyBorder( /* TLRB */ 10, 10, 10, 10) );
/* TLRB means "top, left, bot, right" */
setupPanel.setLayout(new GridLayout(/* rows (height) */ 0, /* cols (width) */ 1));
//paramPanel.setLayout( new FlowLayout() );
setupPanel.setBackground( Color.BLUE );
setupPanel.add( getFileBtn );
setupPanel.add( randomBoardBtn );
JPanel controlPanel = new JPanel( );
controlPanel.setBorder(BorderFactory.createEmptyBorder( /* TLRB */ 10, 10, 10, 10) );
/* using default flowLayout */
JPanel stepPanel = new JPanel( );
stepPanel.setBackground( Color.lightGray );
stepPanel.add( stepOneBtn );
stepPanel.add( stepTenBtn );
stepPanel.add( stepContinuouslyBtn );
controlPanel.add( stepPanel );
controlPanel.add( buildDelayPanel( ) );
controlPanel.add( quitBtn );
//this.getContentadd( "North", paramPanel );
if (myContentPane == null) {
System.out.println("myContentPane is still null");
}
myContentPane.add( new JLabel("page_start"), BorderLayout.PAGE_START);
myContentPane.add( new JLabel("line_start"), BorderLayout.LINE_START);
myContentPane.add( theBoard, BorderLayout.CENTER);
//myContentPane.add("East", paramPanel);
myContentPane.add( setupPanel, BorderLayout.LINE_END);
myContentPane.add( controlPanel, BorderLayout.PAGE_END );
addActionListenersToMyButtons( );
stepperThread.start();
} // buildGUI( )
void addActionListenersToMyButtons( ) {
this.addMouseListener (new MouseAdapter(){
public void mouseClicked(MouseEvent e){
int x = e.getX(), y = e.getY();
System.out.println("mouse clicked on "+x+", "+y);
if(validBoardCoordinates(x,y)) changeCellVal(getBoardX(x),getBoardY(y));
}
});
getFileBtn.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println("getFileBtn");
myContentPane.remove(theBoard);
newBoardFromFile( );
// no, bad! myContentPane.removeAll();
myContentPane.add( theBoard, BorderLayout.CENTER);
myContentPane.validate();
myContentPane.repaint();
}
}); // end of addActionListener
randomBoardBtn.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println("randomBoard");
myContentPane.remove(theBoard);
theBoard = new LifeBoard( 16, 16 );
theBoard.setBackground( Color.GREEN );
// no, bad! myContentPane.removeAll();
myContentPane.add( theBoard, BorderLayout.CENTER);
//theBoard.paintImmediately(theBoard.getVisibleRect());
myContentPane.validate();
//myContentPane.repaint();
stepContinuouslyBtn.setText("Run");
/* [ ] ?? we should have variable for what kind of stepping !! */
repaint();
}
}); // end of addActionListener
stepOneBtn.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println("step");
lifeStep( 1 );
}
}); // end of addActionListener
stepTenBtn.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println("step10");
lifeStep( 10 );
}
}); // end of addActionListener
stepContinuouslyBtn.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println("stepContinuously");
stepContinuously();
}
}); // end of addActionListener
quitBtn.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println("quit");
System.exit( 0 );
}
}); // end of addActionListener
} // addActionListenersToMyButtons( )
/**
* Initialize the delay slider and decorate it!
*/
JPanel buildDelayPanel( ) {
delaySlider = new JSlider(JSlider.HORIZONTAL, /*min:*/0, /*max*/200, /*startVal:*/0);
JPanel delayPanel = new JPanel( );
delayPanel.add( new JLabel("Delay:") );
delayPanel.add( delaySlider );
delaySlider.setToolTipText("Milliseconds delay...");
//Turn on labels at major tick marks.
delaySlider.setMajorTickSpacing(100);
delaySlider.setMinorTickSpacing(10);
delaySlider.setPaintTicks(true);
delaySlider.setPaintLabels(true);
return delayPanel;
} /* buildDelayPanel( ) */
/*
Don't sub-components automatically get painted?
public void paint( Graphics g ) {
this.setVisible( true); // necessary??
if ( theBoard != null ) {
theBoard.paint( g );
// theBoard.repaint( ); // which is better??
}
} // paint( )
*/
void newBoardFromFile( ) {
try {
myContentPane.remove(theBoard);
theBoard = new LifeBoard( /* to find parent frame */ this );
resize( theBoard.wholePictureWidth, theBoard.wholePictureHeight );
// no, bad! myContentPane.removeAll();
myContentPane.add( theBoard, BorderLayout.CENTER);
/* myContentPane.*/validate();
/* myContentPane.*/repaint();
} catch( Exception ex ) {
System.out.println( "Error '" + ex + "' while reading file" );
}
repaint();
stepContinuouslyBtn.setText("Run");
} //newBoardFromFile
/**
* From Gavin
* [ ] ??? We should have variable, not use button name as flag!
*/
void stepContinuously(){
if (stepContinuouslyBtn.getText().equals("Pause")) {
stepContinuouslyBtn.setText("Run");
} else {
stepContinuouslyBtn.setText("Pause");
}
} /* stepContinuously( ) */
/**
* Tells the board to live life for a few turns.
*/
void lifeStep ( int howManySteps ) {
for ( int i = 0; i < howManySteps; ++i ) {
theBoard.step();
// theBoard.quickDraw( g ); // need to get g from somewhere!
//System.out.println( i + " " );;
} // for howManySteps
repaint( );
} // lifeStep()
} // Class LifeGame