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