forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCell.java
More file actions
77 lines (67 loc) · 1.47 KB
/
Cell.java
File metadata and controls
77 lines (67 loc) · 1.47 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
import java.awt.Color;
import java.awt.Graphics;
/**
* A square at a fixed location that changes color.
*
* @author Chris Mayfield
* @version 7.1.0
*/
public class Cell {
public static final Color[] COLORS = {Color.WHITE, Color.BLACK};
private final int x;
private final int y;
private final int size;
private int state;
/**
* Constructs a new cell, initially turned off.
*
* @param x the X coordinate
* @param y the Y coordinate
* @param size number of pixels
*/
public Cell(int x, int y, int size) {
this.x = x;
this.y = y;
this.size = size;
this.state = 0;
}
/**
* Draws the cell on the screen.
*
* @param g graphics context
*/
public void draw(Graphics g) {
g.setColor(COLORS[state]);
g.fillRect(x + 1, y + 1, size - 1, size - 1);
g.setColor(Color.LIGHT_GRAY);
g.drawRect(x, y, size, size);
}
/**
* Tests whether the cell is off.
*
* @return true if the cell is off
*/
public boolean isOff() {
return state == 0;
}
/**
* Tests whether the cell is on.
*
* @return true if the cell is on
*/
public boolean isOn() {
return state == 1;
}
/**
* Sets the cell's state to off.
*/
public void turnOff() {
state = 0;
}
/**
* Sets the cell's state to on.
*/
public void turnOn() {
state = 1;
}
}