-
Notifications
You must be signed in to change notification settings - Fork 0
/
DotsBoxes.java
84 lines (74 loc) · 2.56 KB
/
DotsBoxes.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package demo1;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class DotsBoxes {
// private List<Edge> edges = new ArrayList<>();
// private List<Dot> dots = new ArrayList<>();
private ArrayList<Edge> edges = new ArrayList<>();
private ArrayList<Dot> dots = new ArrayList<>();
private Color currentColor = Color.RED;
public DotsBoxes(int canvasWidth, int canvasHeight) {
StdDraw.setCanvasSize(canvasWidth, canvasHeight);
StdDraw.enableDoubleBuffering();
StdDraw.setXscale(0, 300);
StdDraw.setYscale(0, 300);
initialize();
}
public void initialize() {
edges.add(new Edge(75, 70, 150, 10));
edges.add(new Edge(75, 220, 150, 10));
edges.add(new Edge(70, 75, 10, 150));
edges.add(new Edge(220, 75, 10, 150));
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
dots.add(new Dot(75 + 150 * i, 75 + 150 * j, 15));
}
}
}
public void update() {
Point mousePoint = new Point((int) StdDraw.mouseX(), (int) StdDraw.mouseY());
boolean isMousePressed = StdDraw.isMousePressed();
boolean foundEdge = false;
for (Edge edge : edges) {
if (edge.isFree()) {
if (!foundEdge && edge.getBounds().contains(mousePoint)) {
edge.setColor(currentColor);
edge.setVisible(true);
if (isMousePressed) {
currentColor = currentColor == Color.RED ? Color.BLUE : Color.RED;
edge.setFree(false);
}
foundEdge = true; // Avoid multiple selections.
} else {
edge.setVisible(false);
}
}
}
}
public void paint() {
StdDraw.clear();
// Paint edges first, so dots will be on the top layer.
for (Edge edge: edges) {
edge.paint();
}
for (Dot dot: dots) {
dot.paint();
}
// edges.forEach(Edge::paint);
// dots.forEach(Dot::paint);
StdDraw.show();
}
public static void main(String[] args) {
DotsBoxes game = new DotsBoxes(400, 400);
while (true) {
game.update();
game.paint();
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}