// file: GSmartSquare.java // date: 3/23/2014 // authors: Andrew Mertz and Nancy Van Cleave // // This class represents a square graphical object that can tell when it has // overflowed the window it is displayed in, and is filled. It is derived from the // GSquare class, so will have a color and stay square. package graphics; import acm.graphics.*; import java.awt.*; public class GSmartSquare extends GSquare { // DATA MEMBER // myCanvas allows this object to be aware of to which graphics window it belongs, // making it easier to interrogate the window for dimensions, etc. // It is made protected to allow objects of derived classes access to it. protected GCanvas myCanvas; // CONSTRUCTORS -- construct the inner GRect, setFilled(), and initialize data // member myCanvas // // Constructs a new square with the specified width and height, positioned at // (x,y), and colored the given color (note that the square is filled). // The parent class is GSquare, so super() invokes a GSquare constructor public GSmartSquare(double x, double y, double size, Color color, GCanvas screen) { super(x, y, size, color); setFilled(true); myCanvas = screen; } // Constructs a new square with the specified width and height and positioned // at (x,y), with a default color of black public GSmartSquare(double x, double y, double size, GCanvas screen) { this(x, y, size, Color.BLACK, screen); } // Constructs a new square with the specified width and height and positioned // at the origin and with the default color (black). public GSmartSquare(double size, GCanvas screen) { this(0.0, 0.0, size, screen); } // FACILITATOR METHODS WHICH MAKE THIS OBJECT "SMART" // Determines whether this GSmartSquare extends past the left or right // edges of its containing window. public boolean overflowsWindowHorizontally() { /* Make sure that we have a canvas. If not, there is nothing for us to * overflow. */ if (myCanvas == null) { return false; } // We have a canvas, so check if this object is leaving the window on // the left or right. return (getX() <= 0.0) || (getX() + getWidth() >= myCanvas.getWidth()); } /*************************************************************************** * Add documentation comment here ***************************************************************************/ public boolean overflowsWindowVertically() { /************************************************************************* * replace code here *************************************************************************/ return false; // <== only here to prevent red complaints from Netbeans } /*************************************************************************** * Add documentation comment here ***************************************************************************/ public boolean fitsInItsWindow() { /************************************************************************* * replace code here *************************************************************************/ return false; // <== only here to prevent red complaints from Netbeans } }