-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathArc.java
More file actions
347 lines (308 loc) · 8.15 KB
/
Copy pathArc.java
File metadata and controls
347 lines (308 loc) · 8.15 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import java.awt.Graphics;
import java.awt.Color;
/**
* An arc that can be manipulated and that draws itself on a canvas.
*
* @author Michael Kölling and David J. Barnes and Brian Dahlem
* @version 2018.11.26
*/
public class Arc
{
private int diameter;
private int xPosition;
private int yPosition;
private int startAngle;
private int extent;
private Color color;
private boolean isVisible;
/**
* Create a new arc at default position with default color.
*/
public Arc()
{
diameter = 68;
xPosition = 130;
yPosition = 75;
startAngle = 30;
extent = 120;
color = Canvas.getColor("magenta");
}
/**
* Create an arc at a given position with specified color and shape
* @param x the x location of the Arc
* @param y the y location of the Arc
* @param diameter the diameter of the Arc
* @param arcStartAngle the angle the arc sweep starts at
* @param arcEndAngle the angle the arc sweep ends at
* @param color the name of the color for the Arc
* @param visible true displays the Arc on the canvas
*/
public Arc(int x, int y, int diameter, int arcStartAngle, int arcEndAngle,
String color, boolean visible){
xPosition = x;
yPosition = y;
this.diameter = diameter;
this.startAngle = arcStartAngle;
extent = arcEndAngle - arcStartAngle;
extent %= 360;
if (extent < 0) {
extent += 360;
}
this.color = Canvas.getColor(color);
if (visible) {
makeVisible();
}
}
/**
* Make this arc visible. If it was already visible, do nothing.
*/
public void makeVisible()
{
if (!isVisible) {
isVisible = true;
add();
}
}
/**
* Make this arc invisible. If it was already invisible, do nothing.
*/
public void makeInvisible()
{
if (isVisible){
remove();
isVisible = false;
}
}
/**
* Determine if the arc should be showing on the canvas
* @return true if the shape is not hidden
*/
public boolean isVisible()
{
return isVisible;
}
/**
* Move the arc a few pixels to the right.
*/
public void moveRight()
{
moveHorizontal(20);
}
/**
* Move the arc a few pixels to the left.
*/
public void moveLeft()
{
moveHorizontal(-20);
}
/**
* Move the arc a few pixels up.
*/
public void moveUp()
{
moveVertical(-20);
}
/**
* Move the arc a few pixels down.
*/
public void moveDown()
{
moveVertical(20);
}
/**
* Move the arc horizontally by 'distance' pixels.
* @param distance the distance to move along the x axis,
* positive to the right
*/
public void moveHorizontal(int distance)
{
xPosition += distance;
}
/**
* Move the arc vertically by 'distance' pixels.
* @param distance the distance to move along the y axis, positive down
*/
public void moveVertical(int distance)
{
yPosition += distance;
}
/**
* Move the arc horizontally to a given X location
* @param xPosition the new X location to move the arc to
*/
public void setX(int xPosition)
{
this.xPosition = xPosition;
}
/**
* Move the arc vertically to a given Y location
* @param yPosition the new Y location to move the arc to
*/
public void setY(int yPosition)
{
this.yPosition = yPosition;
}
/**
* Move the arc to a given (X, Y) coordinate
* @param x the new X location to move to
* @param y the new Y location to move to
*/
public void setPosition(int x, int y)
{
xPosition = x;
yPosition = y;
}
/**
* Determine the current X location of the arc
* @return the current X location of the arc
*/
public int getX()
{
return xPosition;
}
/**
* Determine the current Y location of the arc
* @return the current Y location of the arc
*/
public int getY()
{
return xPosition;
}
/**
* Open the arc a bit.
*/
public void openArc()
{
startAngle += 10;
if (startAngle >= 180) {
startAngle = 180;
}
extent = 360 - (2 * startAngle);
}
/**
* Close the arc a bit.
*/
public void closeArc()
{
startAngle -= 10;
if (startAngle < 0) {
startAngle = 0;
}
extent = 360 - (2 * startAngle);
}
/**
* Change the beginning angle of the arc
* @param angle the angle at which the arc should start, 0° to the
* right, increasing counterclockwise
*/
public void changeArcBeginning(int angle)
{
startAngle = angle;
}
/**
* Change the ending angle of the arc
* @param angle the angle at which the arc should end, 0° to the right,
* increasing counterclockwise
*/
public void changeArcEnd(int angle)
{
extent = angle - startAngle;
extent %= 360;
if (extent < 0) {
extent += 360;
}
}
/**
* Determine the angle at which the arc starts
* @return the angle (in degrees) of the beginning of the arc, 0° to
* the right, increasing counterclockwise
*/
public int getArcBeginning()
{
return startAngle;
}
/**
* Determine the angle at which the arc ends
* @return the angle (in degrees) of the end of the arc, 0° to the
* right, increasing counterclockwise
*/
public int getArcEnd()
{
return startAngle + extent;
}
/**
* Determine the angle enclosed by the arc
* @return the angle (in degrees) the arc traces counterclockwise
*/
public int getArcLength()
{
return extent;
}
/**
* Change the size of the arc to the new size (in pixels). Size must be >= 0.
* @param newDiameter the new diameter for the arc
*/
public void changeSize(int newDiameter)
{
diameter = newDiameter;
}
/**
* Determine the current diameter of the arc
* @return the current diameter of the arc
*/
public int getDiameter()
{
return diameter;
}
/**
* Change the color. Valid colors are "red", "yellow", "blue", "green",
* "magenta", "cyan", "brown", "white", and "black", or rgb hex strings
* "#rrggbb" where rr, gg, bb are 2-hexit values for red, green, and
* blue levels.
* @param newColor a lower case string naming the color for the arc
*/
public void changeColor(String newColor)
{
color = Canvas.getColor(newColor);
}
/**
* Draw an arc with current specifications on the screen.
*/
private void add()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.add(this, (g) -> {g.setColor(color);
g.fillArc(xPosition, yPosition,
diameter, diameter,
startAngle, extent);});
}
}
/**
* Remove the arc from the screen.
*/
private void remove()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.remove(this);
}
}
/**
* Get a text description of the arc.
*/
public String toString() {
String visibility;
if (isVisible) {
visibility = "Visible";
}
else {
visibility = "Invisible";
}
return visibility + " Arc of diameter " + this.diameter +
" at (" + this.xPosition + ", " + this.yPosition + ")" +
" starting at " + this.startAngle + "degrees, " +
this.extent + " degrees wide," +
" with color " + this.color;
}
}