forked from asphaltpanthers/SlowLifeGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCell.java
More file actions
92 lines (76 loc) · 1.85 KB
/
Cell.java
File metadata and controls
92 lines (76 loc) · 1.85 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
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Cell extends JButton {
private boolean _beenAlive = false;
private int _maxSize = 10000;
public Cell() {
super(" ");
setFont(new Font("Courier", Font.PLAIN, 12));
addActionListener(new CellButtonListener());
}
public Cell(boolean alive) {
super(" ");
setFont(new Font("Courier", Font.PLAIN, 12));
addActionListener(new CellButtonListener());
setAlive(alive);
}
public void resetBeenAlive() {
_beenAlive = false;
}
public void reset() {
resetBeenAlive();
setAlive(false);
}
public boolean getAlive() {
String text = getText();
return (text.equals("X"));
}
public String toString() {
String toReturn = new String("");
String currentState = getText();
for (int j = 0; j < _maxSize; j++) {
toReturn += currentState;
}
if (toReturn.substring(0,1).equals("X")) {
return toReturn.substring(0,1);
} else {
return ".";
}
}
public void setAlive(boolean a) {
// note that "if (a)" and "if (a == true)"
// really say the same thing!
if (a) {
_beenAlive = true;
setText("X");
setBackground(Color.RED);
} else {
setText(" ");
if (_beenAlive) {
setBackground(Color.GREEN);
} else {
setBackground(Color.GRAY);
}
}
setContentAreaFilled(true);
setOpaque(true);
}
class CellButtonListener implements ActionListener {
// Every time we click the button, it will perform
// the following action.
public void actionPerformed(ActionEvent e) {
Cell source = (Cell) e.getSource();
String currentText = source.getText();
resetBeenAlive();
if (currentText.equals(" ")) {
setAlive(true);
} else if (currentText.equals("X")) {
setAlive(false);
} else {
// This shouldn't happen
setAlive(false);
}
}
}
}