Source of OldRectangle.java


  1: /**
  2:  * A class representing a rectangle of solid colour.
  3:  *
  4:  * @author Mark Young (A00000000)
  5:  */
  6: public class Rectangle {
  7:     // ---------- Instance variables ---------------------------------------
  8:     private double width;
  9:     private double height;
 10:     private MyColor colour;

 12:     // ---------- Constructors ---------------------------------------------
 13:     /**
 14:      * Create a Rectangle with the given dimensions and colour.
 15:      *
 16:      * @param w width
 17:      * @param h height
 18:      * @param c colour
 19:      */
 20:     public Rectangle(double w, double h, MyColor c) {
 21:         if (w <= 0) {
 22:             throw new IllegalArgumentException("width: " + w);
 23:         }
 24:         if (h <= 0) {
 25:             throw new IllegalArgumentException("height: " + h);
 26:         }
 27:         width = w;
 28:         height = h;
 29:         colour = c;
 30:     }

 32:     /**
 33:      * Create a Rectangle with the given dimensions in black.
 34:      *
 35:      * @param w width
 36:      * @param h height
 37:      */
 38:     public Rectangle(double w, double h) {
 39:         this(w, h, MyColor.BLACK);
 40:     }

 42:     // ---------- Getters --------------------------------------------------
 43:     /**
 44:      * Return this Rectangle's height.
 45:      *
 46:      * @return height of this Rectangle
 47:      */
 48:     public double getHeight() {
 49:         return height;
 50:     }

 52:     /**
 53:      * Return this Rectangle's width.
 54:      *
 55:      * @return width of this Rectangle
 56:      */
 57:     public double getWidth() {
 58:         return width;
 59:     }

 61:     /**
 62:      * Return this Rectangle's colour.
 63:      *
 64:      * @return colour of this Rectangle
 65:      */
 66:     public MyColor getColour() {
 67:         return colour;
 68:     }

 70:     /**
 71:      * Return this Rectangle's area.
 72:      *
 73:      * @return this Rectangle's area
 74:      */
 75:     public double getArea()  {
 76:         return height * width;
 77:     }

 79:     /**
 80:      * Return this Rectangle's perimeter.
 81:      *
 82:      * @return this Rectangle's perimeter
 83:      */
 84:     public double getPerimeter() {
 85:         return 2 * (height + width);
 86:     }

 88:     // ---------- Setters --------------------------------------------------
 89:     /**
 90:      * Change this Rectangle's height.
 91:      *
 92:      * @param newHeight the new height of this Rectangle
 93:      */
 94:     public boolean setHeight(double newHeight) {
 95:         if (newHeight >= 0) {
 96:             height = newHeight;
 97:             return true;
 98:         }
 99:         return false;
100:     }

102:     /**
103:      * Change this Rectangle's width.
104:      *
105:      * @param newWidth the new width of this Rectangle
106:      */
107:     public boolean setWidth(double newWidth) {
108:         if (newWidth >= 0) {
109:             width = newWidth;
110:             return true;
111:         }
112:         return false;
113:     }

115:     /**
116:      * Change this Rectangle's colour.
117:      *
118:      * @param newColour the new colour of this Rectangle
119:      */
120:     public boolean setColour(MyColor newColour) {
121:         colour = newColour;
122:         return true;
123:     }

125:     // ---------- Observers ------------------------------------------------
126:     /**
127:      * Convert this Rectangle to a String.
128:      * NOTE: does not include the colour.
129:      *
130:      * @return a String representing this Rectangle
131:      */
132:     @Override
133:     public String toString() {
134:         return width + "x" + height + " Rectangle";
135:     }

137:     /**
138:      * Render this Rectangle in the middle of a Canvas.
139:      *
140:      * @param theCanvas the canvas to render this Rectangle on
141:      */
142:     public void drawOn(javafx.scene.canvas.Canvas theCanvas) {
143:         javafx.scene.canvas.GraphicsContext brush 
144:                 = theCanvas.getGraphicsContext2D();
145:         double canvasHeight = theCanvas.getHeight();
146:         double canvasWidth = theCanvas.getWidth();
147:         double down = (canvasHeight - height) / 2.0;
148:         double across = (canvasWidth - width) / 2.0;

150:         javafx.scene.paint.Paint oldFill = brush.getFill();
151:         brush.setFill(colour.toJavaFXColor());
152:         brush.fillRect(across, down, width, height);
153:         brush.setFill(oldFill);
154:     }

156: }