// file: GSquare.java // date: 3/23/2014 // authors: Andrew Mertz and Nancy Van Cleave // // This class represents a square graphical object. It is derived from the GRect ACM // class and adds a color and the restriction of width and height to the same measure. // It overrides the super methods which allow a client program to change the dimension, // forcing the shape to remain square. The overridden methods are setSize() and setBounds(). package graphics; import acm.graphics.*; import java.awt.*; public class GSquare extends GRect { // CONSTRUCTORS -- construct the inner GRect, and set the color // // Constructs a new GSquare with the specified width and height, positioned at // (x,y), and colored the given color (note that the square is not filled). // Since the parent class is GRect, a constructor for that class is invoked // with super() and the color is set public GSquare(double x, double y, double size, Color color) { super(x, y, size, size); setColor(color); } // Constructs a new square with the specified width and height and positioned // at (x,y), with a default color of black public GSquare(double x, double y, double size) { this(x, y, size, Color.BLACK); } // Constructs a new square with the specified width and height and positioned // at the origin (and the black default color). public GSquare(double size) { this(0.0, 0.0, size); } // MUTATORS -- setSize() and setBounds() override parent versions /* We want to GSquare to maintain its square shape when resized so we override * the following methods. */ // Changes the length of the sides of the square to the given value. public void setSize(double size) { super.setSize(size, size); } // Changes the length of the sides of the square so that it fits inside the // given values. In other words the new side length is the minimum of the // given width and height. public void setSize(double width, double height) { double min = Math.min(width, height); setSize(min); } // Changes the position and length of the sides of the square so that it fits // inside the given bounding box. In other words the new side length is the // minimum of the given width and height and the new position is (x,y). public void setBounds(double x, double y, double width, double height) { setLocation(x, y); setSize(width, height); } // Changes the position and length of the sides of the square so that it fits // inside the given bounding box. In other words the new side length is the // size and the new position is (x,y). public void setBounds(double x, double y, double size) { setLocation(x, y); setSize(size); } }