|
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.Rectangle2D; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* <p>Draw a rectangle</p> |
|
12
|
|
|
* |
|
13
|
|
|
* <p><img src="doc-files/Rectangle.png" alt="Rectangle diagram"></p> |
|
14
|
|
|
* |
|
15
|
|
|
* @author <a href="https://github.com/gann-cdf/graphics/issues" target="_blank">Seth Battis</a> |
|
16
|
|
|
*/ |
|
17
|
|
|
public class Rectangle extends Drawable2D { |
|
18
|
|
|
/** |
|
19
|
|
|
* <p>Construct a new rectangle</p> |
|
20
|
|
|
* |
|
21
|
|
|
* <p><img src="doc-files/Rectangle.png" alt="Diagram of Rectangle parameters"></p> |
|
22
|
|
|
* |
|
23
|
|
|
* <p>All window coordinates are measured in pixels, with the X-axis increasing from left to right and the Y-axis |
|
24
|
|
|
* increasing from top to bottom. All window coordinates exist in the first quadrant.</p> |
|
25
|
|
|
* |
|
26
|
|
|
* <p><img src="../doc-files/window-coordinates.png" alt="Diagram of window coordinates"></p> |
|
27
|
|
|
* |
|
28
|
|
|
* @param x coordinate of origin |
|
29
|
|
|
* @param y coordinate of origin |
|
30
|
|
|
* @param width in pixels |
|
31
|
|
|
* @param height in pixels |
|
32
|
|
|
* @param drawingPanel on which to draw |
|
33
|
|
|
*/ |
|
34
|
|
|
public Rectangle(double x, double y, double width, double height, DrawingPanel drawingPanel) { |
|
35
|
|
|
try { |
|
36
|
|
|
setShape(new Rectangle2D.Double(x, y, width, height)); |
|
37
|
|
|
setDrawingPanel(drawingPanel); |
|
38
|
|
|
} catch (DrawableException e) { |
|
39
|
|
|
System.err.println(e.getMessage()); |
|
40
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
@Override |
|
45
|
|
|
public void setShape(Shape shape) throws DrawableException { |
|
46
|
|
|
if (shape instanceof Rectangle2D) { |
|
47
|
|
|
super.setShape(shape); |
|
48
|
|
|
} else { |
|
49
|
|
|
throw new DrawableException("Attempt to set Rectangles's underlying shape to a non-Rectangle2D instance"); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|