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
85 lines (73 loc) · 1.66 KB
/
Cell.java
File metadata and controls
85 lines (73 loc) · 1.66 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
import java.awt.Color;
import java.awt.Graphics;
/**
* A square at a fixed location that changes color.
*/
public class Cell {
public static final Color OFF = Color.WHITE;
public static final Color ON = Color.BLACK;
private final int x;
private final int y;
private final int size;
private Color color;
/**
* 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.color = OFF;
}
/**
* Paints the cell on the screen.
*
* @param g graphics context
*/
public void draw(Graphics g) {
g.setColor(this.color);
g.fillRect(this.x + 1, this.y + 1, this.size - 1, this.size - 1);
g.setColor(Color.LIGHT_GRAY);
g.drawRect(this.x, this.y, this.size, this.size);
}
/**
* @return the cell's color
*/
public Color getColor() {
return this.color;
}
/**
* @param color the new color
*/
public void setColor(Color color) {
this.color = color;
}
/**
* @return true if the cell is on
*/
public boolean isOff() {
return this.color == OFF;
}
/**
* @return true if the cell is off
*/
public boolean isOn() {
return this.color == ON;
}
/**
* Sets the cell's color to OFF.
*/
public void turnOff() {
this.color = OFF;
}
/**
* Sets the cell's color to ON.
*/
public void turnOn() {
this.color = ON;
}
}