-
Notifications
You must be signed in to change notification settings - Fork 0
/
Visualizer.java
251 lines (197 loc) · 8.91 KB
/
Visualizer.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package com.niklashalle;
import edu.princeton.cs.introcs.Draw;
import edu.princeton.cs.introcs.DrawListener;
import java.awt.Color;
@SuppressWarnings("FieldCanBeLocal")
public class Visualizer {
// all lengths in mm
private final double CANVAS_BORDER = 2000;
private final Color BLACK = new Color(0x000000);
private final Color WHITE = new Color(0xFFFFFF);
private final Color BLUE = new Color(0x0000FF);
private final Color YELLOW = new Color(0xFFFF00);
private final Color RED = new Color(0xFF0000);
private final Color CYAN = new Color(0x00FFFF);
private final Color GREEN = new Color(0x00FF00);
private final Color MAGENTA = new Color(0xFF00FF);
// field
private final Color WEDGE_GREEN = new Color(0x008844);
private final Color FIELD_GREEN = new Color(0x328700);
private final double FIELD_WIDTH_HALF = 24300/2d;
private final double FIELD_HEIGHT_HALF = 18200/2d;
private final double WIDTH_HEIGHT_RATIO = FIELD_WIDTH_HALF / FIELD_HEIGHT_HALF;
private final double WEDGE_WIDTH_HALF = FIELD_WIDTH_HALF;
private final double WEDGE_HEIGHT_HALF = FIELD_HEIGHT_HALF;
private final double WEDGE_SIZE = 1000;
private final double INNER_WIDTH_HALF = FIELD_WIDTH_HALF - 3000;
private final double INNER_HEIGHT_HALF = FIELD_HEIGHT_HALF - 3000;
private final double PENALTY_WIDTH_HALF = 3000/2d;
private final double PENALTY_HEIGHT_HALF = 9000/2d;
private final double PENALTY_X = FIELD_HEIGHT_HALF - PENALTY_WIDTH_HALF;
private final double GOAL_WIDTH_HALF = 740/2d;
private final double GOAL_HEIGHT_HALF = 6000/2d;
private final double GOAL_X = INNER_WIDTH_HALF + GOAL_WIDTH_HALF;
private final double NEUTRAL_X = INNER_WIDTH_HALF - 4500;
private final double NEUTRAL_Y = GOAL_HEIGHT_HALF;
private final double MIDDLE_CIRCLE_RADIUS = 6000/2d;
// wall markers
private final double WALL_MARKER_LENGTH = 2100;
// robot
private final double ROBOT_RADIUS = 1100;
private final double ROBOT_CAPZONE_DEPTH = 250;
// ball
private final Color ORANGE = new Color(0xFF8000);
private final double BALL_RADIUS = 650/2d;
// canvas
private final int CANVAS_HEIGHT = 888;
private final int CANVAS_WIDTH = (int) Math.ceil(CANVAS_HEIGHT * WIDTH_HEIGHT_RATIO);
private Draw mCanvas;
private double defaultPenRadius;
private boolean ended = false;
Visualizer() {
initializeCanvas();
}
private void initializeCanvas() {
mCanvas = new Draw();
mCanvas.addListener(new DrawListener() {
@Override
public void mousePressed(double x, double y) {
}
@Override
public void mouseDragged(double x, double y) {
}
@Override
public void mouseReleased(double x, double y) {
}
@Override
public void keyTyped(char c) {
}
@Override
public void keyPressed(int keycode) {
switch (keycode) {
case 'q':
case 'Q':
{
ended = true;
break;
}
default: break;
}
}
@Override
public void keyReleased(int keycode) {
}
});
mCanvas.enableDoubleBuffering();
mCanvas.setCanvasSize(CANVAS_WIDTH, CANVAS_HEIGHT);
mCanvas.setXscale(-FIELD_WIDTH_HALF - CANVAS_BORDER, FIELD_WIDTH_HALF + CANVAS_BORDER);
mCanvas.setYscale(-FIELD_HEIGHT_HALF - CANVAS_BORDER, FIELD_HEIGHT_HALF + CANVAS_BORDER);
defaultPenRadius = mCanvas.getPenRadius() * 3 / 512;
mCanvas.setPenRadius(defaultPenRadius);
mCanvas.setLocationOnScreen((int) ((1920/2d) - (CANVAS_WIDTH/2d)), (int) ((1080/2d) - (CANVAS_HEIGHT/2d)) - 25);
}
void drawBackground() {
// background
mCanvas.setPenRadius(defaultPenRadius);
mCanvas.setPenColor(BLACK);
mCanvas.filledRectangle(0, 0,
FIELD_WIDTH_HALF + CANVAS_BORDER, FIELD_HEIGHT_HALF + CANVAS_BORDER);
// wedge
mCanvas.setPenRadius(defaultPenRadius);
mCanvas.setPenColor(WEDGE_GREEN);
mCanvas.filledRectangle(0, 0, WEDGE_WIDTH_HALF, WEDGE_HEIGHT_HALF);
// inner
mCanvas.setPenRadius(defaultPenRadius);
mCanvas.setPenColor(FIELD_GREEN);
mCanvas.filledRectangle(0, 0, WEDGE_WIDTH_HALF - WEDGE_SIZE, WEDGE_HEIGHT_HALF - WEDGE_SIZE);
// wall markers
mCanvas.setPenRadius(defaultPenRadius * 1.75);
mCanvas.setPenColor(RED);
mCanvas.line(-FIELD_WIDTH_HALF, FIELD_HEIGHT_HALF, -FIELD_WIDTH_HALF + WALL_MARKER_LENGTH, FIELD_HEIGHT_HALF);
mCanvas.line(0, FIELD_HEIGHT_HALF, 0 + WALL_MARKER_LENGTH, FIELD_HEIGHT_HALF);
mCanvas.line(-FIELD_WIDTH_HALF, FIELD_HEIGHT_HALF, -FIELD_WIDTH_HALF, FIELD_HEIGHT_HALF - WALL_MARKER_LENGTH);
mCanvas.setPenColor(CYAN);
mCanvas.line(FIELD_WIDTH_HALF - WALL_MARKER_LENGTH, FIELD_HEIGHT_HALF, FIELD_WIDTH_HALF, FIELD_HEIGHT_HALF);
mCanvas.line(-WALL_MARKER_LENGTH, FIELD_HEIGHT_HALF, 0, FIELD_HEIGHT_HALF);
mCanvas.line(FIELD_WIDTH_HALF, FIELD_HEIGHT_HALF, FIELD_WIDTH_HALF, FIELD_HEIGHT_HALF - WALL_MARKER_LENGTH);
mCanvas.setPenColor(GREEN);
mCanvas.line(-FIELD_WIDTH_HALF + WALL_MARKER_LENGTH, -FIELD_HEIGHT_HALF, -FIELD_WIDTH_HALF, -FIELD_HEIGHT_HALF);
mCanvas.line(0, -FIELD_HEIGHT_HALF, WALL_MARKER_LENGTH, -FIELD_HEIGHT_HALF);
mCanvas.line(-FIELD_WIDTH_HALF, -FIELD_HEIGHT_HALF, -FIELD_WIDTH_HALF, -FIELD_HEIGHT_HALF + WALL_MARKER_LENGTH);
mCanvas.setPenColor(MAGENTA);
mCanvas.line(FIELD_WIDTH_HALF - WALL_MARKER_LENGTH, -FIELD_HEIGHT_HALF, FIELD_WIDTH_HALF, -FIELD_HEIGHT_HALF);
mCanvas.line(-WALL_MARKER_LENGTH, -FIELD_HEIGHT_HALF, 0, -FIELD_HEIGHT_HALF);
mCanvas.line(FIELD_WIDTH_HALF, -FIELD_HEIGHT_HALF, FIELD_WIDTH_HALF, -FIELD_HEIGHT_HALF + WALL_MARKER_LENGTH);
}
void drawRobot(double x, double y, double t, String name) {
// body
mCanvas.setPenColor(Color.BLACK);
mCanvas.setPenRadius(defaultPenRadius);
mCanvas.filledCircle(x, y, ROBOT_RADIUS);
// "cap zone"
mCanvas.setPenColor(FIELD_GREEN);
mCanvas.setPenRadius(defaultPenRadius*1.65);
double angle = Math.toRadians(t - 45);
double x1 = x + ROBOT_RADIUS - BALL_RADIUS - x;
double y1 = y + ROBOT_RADIUS - BALL_RADIUS - y;
double x2 = x1 * Math.cos(angle) - y1 * Math.sin(angle);
double y2 = x1 * Math.sin(angle) + y1 * Math.cos(angle);
double newX = x2 + x;
double newY = y2 + y;
for (int i = (int) ROBOT_CAPZONE_DEPTH; i >= 0; --i) {
mCanvas.arc(newX, newY, i, t + 112, t - 112);
}
// marker
mCanvas.setPenColor(WHITE);
mCanvas.text(x, y, name);
}
void drawBallLine(double x0, double y0, double x1, double y1) {
mCanvas.setPenRadius(defaultPenRadius/3);
mCanvas.setPenColor(ORANGE);
mCanvas.line(x0, y0, x1, y1);
}
void drawBall(double x, double y) {
mCanvas.setPenColor(ORANGE);
mCanvas.filledCircle(x, y, BALL_RADIUS);
}
void drawField() {
// penalty area
mCanvas.setPenRadius(defaultPenRadius);
mCanvas.setPenColor(BLACK);
mCanvas.rectangle(-PENALTY_X, 0, PENALTY_WIDTH_HALF, PENALTY_HEIGHT_HALF);
mCanvas.rectangle(+PENALTY_X, 0, PENALTY_WIDTH_HALF, PENALTY_HEIGHT_HALF);
// bound lines
mCanvas.setPenRadius(defaultPenRadius * 1.5);
mCanvas.setPenColor(WHITE);
mCanvas.rectangle(0, 0, INNER_WIDTH_HALF, INNER_HEIGHT_HALF);
// goals
mCanvas.setPenRadius(defaultPenRadius * 0.75);
mCanvas.setPenColor(BLACK);
mCanvas.rectangle(+GOAL_X, 0, GOAL_WIDTH_HALF, GOAL_HEIGHT_HALF);
mCanvas.rectangle(-GOAL_X, 0, GOAL_WIDTH_HALF, GOAL_HEIGHT_HALF);
// goal colors
mCanvas.setPenRadius(defaultPenRadius * 1.75);
mCanvas.setPenColor(BLUE);
mCanvas.line(-GOAL_X + GOAL_WIDTH_HALF, GOAL_HEIGHT_HALF, -GOAL_X + GOAL_WIDTH_HALF, -GOAL_HEIGHT_HALF);
mCanvas.setPenColor(YELLOW);
mCanvas.line(GOAL_X - GOAL_WIDTH_HALF, GOAL_HEIGHT_HALF, GOAL_X - GOAL_WIDTH_HALF, -GOAL_HEIGHT_HALF);
// neutral spots
mCanvas.setPenRadius(defaultPenRadius * 2);
mCanvas.setPenColor(BLACK);
mCanvas.point(0, 0);
mCanvas.point(+NEUTRAL_X, +NEUTRAL_Y);
mCanvas.point(-NEUTRAL_X, +NEUTRAL_Y);
mCanvas.point(+NEUTRAL_X, -NEUTRAL_Y);
mCanvas.point(-NEUTRAL_X, -NEUTRAL_Y);
// middle circle
mCanvas.setPenRadius(defaultPenRadius);
mCanvas.setPenColor(BLACK);
mCanvas.circle(0,0, MIDDLE_CIRCLE_RADIUS);
}
void updateScreen() {
mCanvas.show();
}
boolean isEnded() {
return ended;
}
}