-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLifeGame.java
More file actions
executable file
·492 lines (423 loc) · 18.6 KB
/
LifeGame.java
File metadata and controls
executable file
·492 lines (423 loc) · 18.6 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//
// <applet code=LifeGame width=100 height=50>
// </applet>
//
// 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.*;
import javax.swing.event.*;
import java.text.NumberFormat;
import java.net.URI;
import java.net.URL;
/**
* @author Mike Roam et al.
*/
public class LifeGame extends JApplet implements Runnable /* was Applet */ {
public LifeGame me;
private static final long serialVersionUID = 127847834890178391L; // 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;
private final JFileChooser fc; // = new JFileChooser();
private static final String[] rulesList={
"ConwayRules",
"OurRules",
"HalfDeadRules",
};
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 saveBoardBtn = new JButton( "Save Board" );
JButton randomBoardBtn = new JButton( "Random Board" );
//Button randomBoardBtn = new Button( "Random Board" );
JButton blankBoardBtn = new JButton( "Blank 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" );
JLabel generationCount = new JLabel("0");
JComboBox ruleChooser = new JComboBox( rulesList );
JSlider delaySlider = null; /* is instantiated in buildDelayPanel( ) */
public boolean running = false;
Thread stepperThread = new Thread(this);
/**
* default constructor, called by main
*/
public LifeGame( ) {
fc = new JFileChooser();
myContentPane = this.getContentPane( );
me=this;
} // end of default constructor
public static void putInWindow(LifeGame lg) throws Exception{
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JFrame frame = new JFrame(" Life Game ");
frame.getContentPane().add( lg );
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! bad idea to call too much from constructor
lg.init();
lg.start( ); // what's this??
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) throws Exception {
/*UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
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! bad idea to call too much from constructor
myLifeGame.init();
myLifeGame.start( ); // what's this??
frame.pack();
frame.setVisible(true);*/
putInWindow(new LifeGame());
} // 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(30, 30 , new HalfDeadRules(),true );
final LifeGame me = this;
try{
java.security.AccessController.doPrivileged(new java.security.PrivilegedExceptionAction() {
public Object run() throws Exception {
me.buildGUI( );
return null;
}
});
}catch(Exception e){e.printStackTrace();System.exit(1);}
//this.buildGUI( );
} // init( )
public int getBoardX(int x){
return x;
}
public int getBoardY(int y){
return 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
/*//setup rule menu
for(int i=0;i<rulesList.length;i++){
ruleTypes.add(new JMenuItem(rulesList[i]));
}
//done rule menu*/
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) */ 5, /* cols (width) */ 1));
//paramPanel.setLayout( new FlowLayout() );
setupPanel.setBackground( Color.BLUE );
setupPanel.add( getFileBtn );
setupPanel.add( saveBoardBtn );
setupPanel.add( randomBoardBtn );
setupPanel.add( blankBoardBtn );
setupPanel.add( ruleChooser );
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 );
ruleChooser.setSelectedIndex(2);
//this.getContentadd( "North", paramPanel );
if (myContentPane == null) {
System.out.println("myContentPane is still null");
}
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new GridLayout(1,2));
infoPanel.add(new JLabel("Generations: "));
infoPanel.add(generationCount);
myContentPane.add( infoPanel, 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(){ //This has been moved to the lifeboard class. It should handle its own clicks.
public void mouseClicked(MouseEvent e){
int x = e.getX(), y = e.getY();
System.out.println("mouse clicked on "+x+", "+y);
int bx=getBoardX(x), by=getBoardY(y);
if(bx!=-1&&by!=-1) 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();
for(int i=0;i<rulesList.length;i++){
if(rulesList[i]==theBoard.getRulesName()){
ruleChooser.setSelectedIndex(i);
}
}
repaint();
}
}); // end of addActionListener
saveBoardBtn.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println("saveBoardBtn");
int returnVal = fc.showSaveDialog(me);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try{
file.delete();
file.createNewFile();
}catch(Exception exc){
throw new RuntimeException(exc);
}
theBoard.makeFileFromBoard(file.getPath());
} else {
System.out.println("Open command cancelled by user.");
}
}
}); // end of addActionListener
randomBoardBtn.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println("randomBoard");
me.stepContinuously(false);
String currentSize = theBoard.cellsAcross.toString() + "," + theBoard.cellsDown.toString();
int[] newDimensions = DimensionsInputDialog.showDialog(me, currentSize);
if(newDimensions==null) return;
myContentPane.remove(theBoard);
theBoard = new LifeBoard( newDimensions, selectedRules(),true );
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 !! */
me.repaint();
}
}); // end of addActionListener
blankBoardBtn.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println("blankBoard");
me.stepContinuously(false);
String currentSize = theBoard.cellsAcross.toString() + "," + theBoard.cellsDown.toString();
int[] newDimensions = DimensionsInputDialog.showDialog(me, currentSize);
if(newDimensions==null) return;
myContentPane.remove(theBoard);
theBoard = new LifeBoard( newDimensions, selectedRules(),false); //Should cache the previous board size
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 !! */
me.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
ruleChooser.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent ae){
System.out.println("ruleChooser");
/*myContentPane.remove(theBoard);
*
* theBoard = new LifeBoard( 16, 16, selectedRules() );
* 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();*/
me.stepContinuously(false);
theBoard.setRules(selectedRules());
repaint();
}
});
} // addActionListenersToMyButtons( )
public Rules selectedRules(){
String ruleName = ruleChooser.getSelectedItem().toString();
return getRulesFromString(ruleName);
}
private Rules getRulesFromString(String ruleName){
Class maybeRulesClass;
try{
maybeRulesClass = Class.forName(ruleName);
}catch(Exception e){
throw new RuntimeException(ruleName+" is not a valid Rules");
}
Object maybeRules;
try{
maybeRules = maybeRulesClass.newInstance();
}catch(Exception e){
throw new RuntimeException(maybeRulesClass+" could not be instantialized");
}
if(!(maybeRules instanceof Rules)) throw new RuntimeException(maybeRulesClass+" is not a valid Rules");
return (Rules)maybeRules;
}
/**
* Initialize the delay slider and decorate it!
*/
JPanel buildDelayPanel( ) {
delaySlider = new JSlider(JSlider.HORIZONTAL, /*min:*/0, /*max*/200, /*startVal:*/15);
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? yes they do
public void paint( Graphics g ) {
this.setVisible( true); // necessary??
if ( theBoard != null ) {
theBoard.paint( g );
// theBoard.repaint( ); // which is better?? use repaint, it just sets a "dirty flag", so it gets repainted soon
}
} // paint( )
*/
void newBoardFromFile( ) {
try {
this.stepContinuously(false);
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" );
ex.printStackTrace();
}
repaint();
//stepContinuouslyBtn.setText("Run");
} //newBoardFromFile
/**
* From Gavin
* [Ã] ??? We should have variable, not use button name as flag!
*/
void stepContinuously(){
stepContinuously(!running);
} /* stepContinuously( ) */
/**
* change if the board is stepping or not
*/
void stepContinuously(boolean run){
if (!run) {
stepContinuouslyBtn.setText("Run");
running = false;
} else {
stepContinuouslyBtn.setText("Pause");
running = true;
}
} /* 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
generationCount.setText(NumberFormat.getIntegerInstance().format(theBoard.getGenerationCount()));
repaint( );
} // lifeStep()
/* Runnable Junk */
public void run() {
while(true) {
if (running) {
lifeStep(1);
try {
stepperThread.sleep( delaySlider.getValue() );
} catch( Exception e ) {
e.printStackTrace( System.err );
} /* end of try */
}
else stepperThread.yield();
} /* end of while */
} /* run( ) */
} // Class LifeGame