public class Rectangle
1: /**
2: * A class representing a rectangle of solid colour.
3: *
4: * @author Mark Young (A00000000)
5: */
6: public class Rectangle {
7: // ---------- Class variables ------------------------------------------
8: private static MyColor defaultColour = MyColor.BLACK;
10: // ---------- Instance variables ---------------------------------------
11: private double width;
12: private double height;
13: private MyColor colour;
15: // ---------- Constructors ---------------------------------------------
16: /**
17: * Create a Rectangle with the given dimensions and colour.
18: *
19: * @param w width
20: * @param h height
21: * @param c colour
22: */
23: public Rectangle(double w, double h, MyColor c) {
24: // confirm that requested values are OK
25: rejectNegative(w);
26: rejectNegative(h);
27: rejectNull(c);
29: // save confirmed values
30: width = w;
31: height = h;
32: colour = c;
33: }
35: /**
36: * Create a Rectangle with the given dimensions in the default colour.
37: *
38: * @param w width
39: * @param h height
40: */
41: public Rectangle(double w, double h) {
42: this(w, h, defaultColour);
43: }
45: // ---------- Getters --------------------------------------------------
46: /**
47: * Return this Rectangle's height.
48: *
49: * @return height of this Rectangle
50: */
51: public double getHeight() {
52: return height;
53: }
55: /**
56: * Return this Rectangle's width.
57: *
58: * @return width of this Rectangle
59: */
60: public double getWidth() {
61: return width;
62: }
64: /**
65: * Return this Rectangle's colour.
66: *
67: * @return colour of this Rectangle
68: */
69: public MyColor getColour() {
70: return colour;
71: }
73: /**
74: * Return this Rectangle's area.
75: *
76: * @return this Rectangle's area
77: */
78: public double getArea() {
79: return height * width;
80: }
82: /**
83: * Return this Rectangle's perimeter.
84: *
85: * @return this Rectangle's perimeter
86: */
87: public double getPerimeter() {
88: return 2 * (height + width);
89: }
91: // ---------- Setters --------------------------------------------------
92: /**
93: * Change this Rectangle's height.
94: *
95: * @param newHeight the new height of this Rectangle
96: */
97: public void setHeight(double newHeight) {
98: rejectNegative(newHeight);
99: height = newHeight;
100: }
102: /**
103: * Change this Rectangle's width.
104: *
105: * @param newWidth the new width of this Rectangle
106: */
107: public void setWidth(double newWidth) {
108: rejectNegative(newWidth);
109: width = newWidth;
110: }
112: /**
113: * Change this Rectangle's colour.
114: *
115: * @param newColour the new colour of this Rectangle
116: */
117: public boolean setColour(MyColor newColour) {
118: colour = newColour;
119: return true;
120: }
122: // ---------- Observers ------------------------------------------------
123: /**
124: * Convert this Rectangle to a String.
125: * NOTE: does not include the colour.
126: *
127: * @return a String representing this Rectangle
128: */
129: @Override
130: public String toString() {
131: return width + "x" + height + " Rectangle";
132: }
134: /**
135: * Get the default colour for rectangles.
136: *
137: * @return the current default colour for Rectangles.
138: */
139: public static MyColor getDefaultColor() {
140: return defaultColour;
141: }
143: /**
144: * Set the default colour for Rectangles. The default cannot be null.
145: *
146: * @param newColor the new default colour for rectangles.
147: */
148: public static void setDefaultColor(MyColor newDefault) {
149: rejectNull(newDefault);
150: defaultColour = newDefault;
151: }
153: // ---------- Class variables ------------------------------------------
154: /**
155: * Throw an exception if the argument is null.
156: *
157: * @param colourRef the value to confirm is non-null.
158: */
159: private static void rejectNull(MyColor colourRef) {
160: if (colourRef == null) {
161: throw new IllegalArgumentException("Colour cannot be null");
162: }
163: }
165: /**
166: * Throw an exception if the argument is negative.
167: *
168: * @param value the value to confirm is non-negative.
169: */
170: private static void rejectNegative(double value) {
171: if (value < 0.0) {
172: throw new IllegalArgumentException("Dimension cannot be negative");
173: }
174: }
176: // ---------- JavaFX support -------------------------------------------
177: /**
178: * Render this Rectangle in the middle of a Canvas.
179: *
180: * @param theCanvas the canvas to render this Rectangle on
181: */
182: public void drawOn(javafx.scene.canvas.Canvas theCanvas) {
183: // get the brush for this canvas
184: javafx.scene.canvas.GraphicsContext brush
185: = theCanvas.getGraphicsContext2D();
187: // find where the top-left corner of the Rectangle needs to go
188: double canvasHeight = theCanvas.getHeight();
189: double canvasWidth = theCanvas.getWidth();
190: double down = (canvasHeight - height) / 2.0;
191: double across = (canvasWidth - width) / 2.0;
193: // save the old fill colour in order to restore it when we're done
194: javafx.scene.paint.Paint oldFill = brush.getFill();
196: // paint the rectangle on the canvas
197: brush.setFill(colour.toJavaFXColor());
198: brush.fillRect(across, down, width, height);
200: // restore the old fill colour
201: brush.setFill(oldFill);
202: }
204: }