-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCellComponent.java
More file actions
104 lines (84 loc) · 2.87 KB
/
CellComponent.java
File metadata and controls
104 lines (84 loc) · 2.87 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
import javax.swing.JComponent;
import java.awt.*;
public class CellComponent extends JComponent {
private static final int DEFAULT_WIDTH = 10;
private static final int DEFAULT_HEIGHT = DEFAULT_WIDTH;
private static final Dimension DEFAULT_DIMENSION = new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
private final int CELL_WIDTH;
private final int CELL_HEIGHT;
private final int BORDER_WIDTH;
private final Dimension CELL_DIMENSION;
private final boolean PRINT_BORDER;
private boolean alive;
public CellComponent() {
this(false, DEFAULT_DIMENSION, true);
}
public CellComponent(boolean alive) {
this(alive, DEFAULT_DIMENSION, true);
}
public CellComponent(boolean alive, boolean printBorder) {
this(alive, DEFAULT_DIMENSION, printBorder);
}
public CellComponent(boolean alive, int width, int height) {
this(alive, new Dimension(width, height), true);
}
public CellComponent(boolean alive, int width, int height, boolean printBorder) {
this(alive, new Dimension(width, height), printBorder);
}
public CellComponent(int width, int height) {
this(false, new Dimension(width, height), true);
}
public CellComponent(int width, int height, boolean printBorder) {
this(false, new Dimension(width, height), printBorder);
}
public CellComponent(boolean alive, Dimension dimensions, boolean printBorder) {
this.alive = alive;
CELL_WIDTH = (int) dimensions.getWidth();
CELL_HEIGHT = (int) dimensions.getHeight();
BORDER_WIDTH = CELL_WIDTH < CELL_HEIGHT ? CELL_WIDTH/5 : CELL_HEIGHT/5;
CELL_DIMENSION = new Dimension(dimensions);
PRINT_BORDER = printBorder;
}
public void setState(boolean alive) {
this.alive = alive;
}
public boolean getState() {
return alive;
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
if (alive) alivePaint(g2);
else deadPaint(g2);
g2.dispose();
}
private void alivePaint(Graphics2D g2) {
g2.setColor(Color.BLACK);
g2.fillRect(0, 0, CELL_WIDTH, CELL_HEIGHT);
borderPaint(g2);
}
private void deadPaint(Graphics2D g2) {
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, CELL_WIDTH, CELL_HEIGHT);
borderPaint(g2);
}
private void borderPaint(Graphics2D g2) {
if (PRINT_BORDER) {
g2.setStroke(new BasicStroke(BORDER_WIDTH) );
g2.setColor(Color.GRAY);
g2.drawRect(0, 0, CELL_WIDTH, CELL_HEIGHT);
}
}
@Override
public Dimension getMinimumSize() {
return CELL_DIMENSION;
}
@Override
public Dimension getPreferredSize() {
return CELL_DIMENSION;
}
@Override
public Dimension getMaximumSize() {
return CELL_DIMENSION;
}
}