-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConwaysComponent.java
More file actions
71 lines (62 loc) · 2.18 KB
/
ConwaysComponent.java
File metadata and controls
71 lines (62 loc) · 2.18 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
import javax.swing.JComponent;
import java.awt.*;
public class ConwaysComponent extends JComponent implements TickListener {
private int cellWidth;
private int cellHeight;
private int compWidth;
private int compHeight;
private Dimension compDimension;
private Culture culture;
private CellComponent[][] components;
private final boolean PRINT_BORDERS;
public ConwaysComponent(Culture culture) {
this(culture, true);
}
public ConwaysComponent(Culture culture, boolean printBorders) {
this.PRINT_BORDERS = printBorders;
this.setCulture(culture);
}
public void setCulture(Culture culture) {
this.culture = culture;
cellWidth = (int) new CellComponent().getPreferredSize().getWidth();
cellHeight = (int) new CellComponent().getPreferredSize().getHeight();
compWidth = cellWidth * culture.getCols();
compHeight = cellHeight * culture.getRows();
compDimension = new Dimension(compWidth, compHeight);
makeCells();
}
private void makeCells() {
this.setLayout(null);
components = new CellComponent[culture.getRows()][culture.getCols()];
for (int row = 0; row < components.length; row++) {
for (int col = 0; col < components[row].length; col++) {
components[row][col] = new CellComponent(culture.isAlive(row, col), PRINT_BORDERS);
this.add(components[row][col]);
components[row][col].setBounds(cellWidth*col, cellHeight*row, cellWidth, cellHeight);
}
}
}
public void tickPerformed() {
repaint();
}
@Override
protected void paintComponent(Graphics g) {
for (int row = 0; row < components.length; row++) {
for (int col = 0; col < components[row].length; col++) {
components[row][col].setState(culture.isAlive(row, col));
}
}
}
@Override
public Dimension getMinimumSize() {
return compDimension;
}
@Override
public Dimension getPreferredSize() {
return compDimension;
}
@Override
public Dimension getMaximumSize() {
return compDimension;
}
}