|
1
|
|
|
package org.gannacademy.cdf.graphics.geom; |
|
2
|
|
|
|
|
3
|
|
|
import org.gannacademy.cdf.graphics.Drawable2D; |
|
4
|
|
|
import org.gannacademy.cdf.graphics.DrawableException; |
|
5
|
|
|
import org.gannacademy.cdf.graphics.ui.DrawingPanel; |
|
6
|
|
|
|
|
7
|
|
|
import java.awt.*; |
|
8
|
|
|
import java.awt.geom.Arc2D; |
|
9
|
|
|
import java.awt.geom.Rectangle2D; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* <p>Draw an arc as a section of an ellipse</p> |
|
13
|
|
|
* |
|
14
|
|
|
* <p><img src="doc-files/Arc.png" alt="Arc diagram"></p> |
|
15
|
|
|
* |
|
16
|
|
|
* <p>Arcs are sections of ellipses, inscribed within their rectangular bounding box.</p> |
|
17
|
|
|
* |
|
18
|
|
|
* @author <a href="https://github.com/gann-cdf/graphics/issues" target="_blank">Seth Battis</a> |
|
19
|
|
|
*/ |
|
20
|
|
|
public class Arc extends Drawable2D { |
|
21
|
|
|
/** |
|
22
|
|
|
* <p>Construct a new arc</p> |
|
23
|
|
|
* |
|
24
|
|
|
* <p><img src="doc-files/Arc.png" alt="Diagram of Arc parameters"></p> |
|
25
|
|
|
* |
|
26
|
|
|
* <p>All window coordinates are measured in pixels, with the X-axis increasing from left to right and the Y-axis |
|
27
|
|
|
* increasing from top to bottom. All window coordinates exist in the first quadrant.</p> |
|
28
|
|
|
* |
|
29
|
|
|
* <p><img src="../doc-files/window-coordinates.png" alt="Diagram of window coordinates"></p> |
|
30
|
|
|
* |
|
31
|
|
|
* @param x coordinate of origin |
|
32
|
|
|
* @param y coordinate of origin |
|
33
|
|
|
* @param width in pixels |
|
34
|
|
|
* @param height in pixels |
|
35
|
|
|
* @param start angle in degrees (measured counter-clockwise from 0° (a.k.a. "east") |
|
36
|
|
|
* @param extent angle in degrees of the arc (measured counter-clockwise from {@code start}) |
|
37
|
|
|
* @param drawingPanel on which to draw |
|
38
|
|
|
*/ |
|
39
|
|
|
public Arc(double x, double y, double width, double height, double start, double extent, DrawingPanel drawingPanel) { |
|
40
|
|
|
try { |
|
41
|
|
|
setShape(new Arc2D.Double(x, y, width, height, start, extent, Arc2D.PIE)); |
|
42
|
|
|
setDrawingPanel(drawingPanel); |
|
43
|
|
|
} catch (DrawableException e) { |
|
44
|
|
|
System.err.println(e.getMessage()); |
|
45
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Underlying {@link Arc2D} geometry |
|
51
|
|
|
* |
|
52
|
|
|
* @return Underlying {@link Arc2D} geometry |
|
53
|
|
|
*/ |
|
54
|
|
|
protected Arc2D getShapeAsArc() { |
|
55
|
|
|
return (Arc2D) getShape(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
@Override |
|
59
|
|
|
public void setShape(Shape shape) throws DrawableException { |
|
60
|
|
|
if (shape instanceof Arc2D) { |
|
61
|
|
|
super.setShape(shape); |
|
62
|
|
|
} else { |
|
63
|
|
|
throw new DrawableException("Attempt to set Arc's underlying shape to a non-Arc2D instance"); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Start angle of arc |
|
69
|
|
|
* |
|
70
|
|
|
* @return Angle measured counter-clockwise in degrees from 0° (East) of start of ellipse segment |
|
71
|
|
|
* @see Arc2D#getAngleStart() |
|
72
|
|
|
*/ |
|
73
|
|
|
public double getAngleStart() { |
|
74
|
|
|
return getShapeAsArc().getAngleStart(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Extent angle of arc |
|
79
|
|
|
* |
|
80
|
|
|
* @return Angle measured counter-clockwise in degrees from start angle determining extent of arc |
|
81
|
|
|
* @see Arc2D#getAngleExtent() |
|
82
|
|
|
*/ |
|
83
|
|
|
public double getAngleExtent() { |
|
84
|
|
|
return getShapeAsArc().getAngleExtent(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Set start angle of arc |
|
89
|
|
|
* |
|
90
|
|
|
* @param start Angle measured counter-clockwise in degrees from 0° (East) of start of ellipse segment |
|
91
|
|
|
* @see Arc2D#setAngleStart(double) |
|
92
|
|
|
*/ |
|
93
|
|
|
public void setAngleStart(double start) { |
|
94
|
|
|
getShapeAsArc().setAngleStart(start); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Set extent angle of arc |
|
99
|
|
|
* |
|
100
|
|
|
* @param extent Angle measured counter-clockwise in degrees from start angle determining extent of arc |
|
101
|
|
|
* @see Arc2D#setAngleExtent(double) |
|
102
|
|
|
*/ |
|
103
|
|
|
public void setAngleExtent(double extent) { |
|
104
|
|
|
getShapeAsArc().setAngleExtent(extent); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
@Override |
|
108
|
|
|
public double getHeight() { |
|
109
|
|
|
return getShapeAsArc().getHeight(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
@Override |
|
113
|
|
|
public double getWidth() { |
|
114
|
|
|
return getShapeAsArc().getWidth(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
@Override |
|
118
|
|
|
public double getX() { |
|
119
|
|
|
return getShapeAsArc().getX(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
@Override |
|
123
|
|
|
public double getY() { |
|
124
|
|
|
return getShapeAsArc().getY(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
@Override |
|
128
|
|
|
public Rectangle2D getBounds() { |
|
129
|
|
|
return getShapeAsArc().getBounds(); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|