-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.java
70 lines (59 loc) · 1.2 KB
/
Cell.java
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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import javax.swing.JButton;
/**
* Class to represent a cell in Conway's Game of Life.
*
* @author Alex Pieczynski
*/
public class Cell extends JButton
{
private static final Color ALIVE_COLOR = Color.yellow;
private static final Color DEAD_COLOR = Color.gray;
private boolean _alive;
private ActionListener _listener;
public Cell()
{
super();
this.setPreferredSize(new Dimension(15,15));
_listener = e ->
{
if (_alive)
kill();
else
spawn();
};
this.reset();
}
public void kill()
{
_alive = false;
setBackground(DEAD_COLOR);
}
public void spawn()
{
_alive = true;
setBackground(ALIVE_COLOR);
}
public boolean isAlive()
{
return _alive;
}
/**
* Disables clicking on the grid so the user cannot kill/spawn cells
* once the simulation has started.
*/
public void onGameStart()
{
this.removeActionListener(_listener);
}
/**
* Puts the cell into its initial state, dead and waiting for clicks.
*/
public void reset()
{
this.addActionListener(_listener);
this.kill();
}
}