-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisplay.java
143 lines (112 loc) · 4.3 KB
/
Display.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import java.awt.*;
import java.util.Iterator;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import javax.swing.*;
// This code handles the graphical display of the board.
// You may assume it works fine.
public class Display extends JPanel {
private int myWindowWidth = 200;
private int myWindowHeight = 220;
private int POINT_RADIUS = 10;
private int [ ] [ ] POINT_POSITIONS
= {{100, 20}, {170, 70}, {170, 130}, {100, 180}, {30, 130}, {30, 70}};
private Board myBoard;
public Display (Board board) {
JFrame myFrame = new JFrame ( );
myFrame.setSize (myWindowWidth, myWindowHeight);
myFrame.add (this);
myFrame.setVisible (true);
myFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
myBoard = board;
}
public void paintComponent (Graphics g) {
// Clear the screen.
g.setColor (Color.WHITE);
g.fillRect (0, 0, myWindowWidth, myWindowHeight);
// Draw the lines connecting the points.
Iterator<Connector> allConnectors = myBoard.connectors ( );
while (allConnectors.hasNext ( )) {
Connector cnctr = allConnectors.next ( );
g.setColor (myBoard.colorOf (cnctr));
int k1 = cnctr.endPt1 ( );
int k2 = cnctr.endPt2 ( );
Graphics2D g2d = (Graphics2D)g;
Line2D lin = new Line2D.Float(POINT_POSITIONS[k1-1][0], POINT_POSITIONS[k1-1][1],
POINT_POSITIONS[k2-1][0], POINT_POSITIONS[k2-1][1]);
g2d.draw(lin);
//g.drawLine (POINT_POSITIONS[k1-1][0], POINT_POSITIONS[k1-1][1],
// POINT_POSITIONS[k2-1][0], POINT_POSITIONS[k2-1][1]);
}
// Draw the lines connecting the points.
Iterator<Connector> freeConnectors = myBoard.connectors ( Color.WHITE);
while (freeConnectors.hasNext ( )) {
Connector cnctr = freeConnectors.next ( );
g.setColor (Color.GRAY);
int k1 = cnctr.endPt1 ( );
int k2 = cnctr.endPt2 ( );
Graphics2D g2d = (Graphics2D)g;
Line2D lin = new Line2D.Float(POINT_POSITIONS[k1-1][0], POINT_POSITIONS[k1-1][1],
POINT_POSITIONS[k2-1][0], POINT_POSITIONS[k2-1][1]);
g2d.draw(lin);
//g.drawLine (POINT_POSITIONS[k1-1][0], POINT_POSITIONS[k1-1][1],
// POINT_POSITIONS[k2-1][0], POINT_POSITIONS[k2-1][1]);
}
Iterator<Connector> redDotted = myBoard.redDotted();
while (redDotted.hasNext())
{
Connector c = redDotted.next();
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] {1,2}, 0));
g2d.setColor(Color.RED);
int k1 = c.endPt1 ( );
int k2 = c.endPt2 ( );
Line2D lin = new Line2D.Float(POINT_POSITIONS[k1-1][0], POINT_POSITIONS[k1-1][1],
POINT_POSITIONS[k2-1][0], POINT_POSITIONS[k2-1][1]);
g2d.draw(lin);
}
Iterator<Connector> blueDotted = myBoard.blueDotted();
while (blueDotted.hasNext())
{
Connector c = blueDotted.next();
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] {1,2}, 0));
g2d.setColor(Color.BLUE);
int k1 = c.endPt1 ( );
int k2 = c.endPt2 ( );
Line2D lin = new Line2D.Float(POINT_POSITIONS[k1-1][0], POINT_POSITIONS[k1-1][1],
POINT_POSITIONS[k2-1][0], POINT_POSITIONS[k2-1][1]);
g2d.draw(lin);
}
Iterator<Connector> dualDotted = myBoard.dualDotted();
while (dualDotted.hasNext())
{
Connector c = dualDotted.next();
Graphics2D g2d = (Graphics2D)g;
int k1 = c.endPt1 ( );
int k2 = c.endPt2 ( );
g2d.setStroke(new BasicStroke(2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 2, new float[] {1,2}, 0));
g2d.setColor(Color.BLUE);
Line2D lin = new Line2D.Float(POINT_POSITIONS[k1-1][0], POINT_POSITIONS[k1-1][1],
POINT_POSITIONS[k2-1][0], POINT_POSITIONS[k2-1][1]);
g2d.draw(lin);
g2d.setStroke(new BasicStroke(2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] {1,2}, 2));
g2d.setColor(Color.RED);
lin = new Line2D.Float(POINT_POSITIONS[k1-1][0], POINT_POSITIONS[k1-1][1],
POINT_POSITIONS[k2-1][0], POINT_POSITIONS[k2-1][1]);
g2d.draw(lin);
}
// Draw the points.
g.setColor (Color.BLACK);
for (int k=0; k<6; k++) {
g.fillOval (POINT_POSITIONS[k][0]-POINT_RADIUS, POINT_POSITIONS[k][1]-POINT_RADIUS,
2*POINT_RADIUS, 2*POINT_RADIUS);
}
// Draw the labels.
g.setColor (Color.WHITE);
for (int k=0; k<6; k++) {
g.drawString (""+(k+1),
POINT_POSITIONS[k][0]-POINT_RADIUS/2, POINT_POSITIONS[k][1]+POINT_RADIUS/2);
}
}
}