// file: TestSmartSquare.java // date: 3/23/2014 // authors: Andrew Mertz and Nancy Van Cleave // // This program should be used to test the GSmartSquare class to ensure it is // working before continuing on to the GMovingSquare class. // // MAX_MOVES times it will create a GSmartSquare object, GSR, and display its coordinates and size. // When the code has been modified, the color of GSR will change depending on whether it's overflowing // left-to-right, top-to-bottom, both, or neither. package testing; import acm.graphics.*; import acm.program.*; import acm.util.*; import java.awt.*; import graphics.*; public class TestSmartSquare extends GraphicsProgram { public void init() { setSize(600, 600); } public void run() { // Create and display a series of GSmartSquare objects for (int i = 1; i <= MAX_SQUARES; i++) { // Choose the size of the square randomly double size = rgen.nextDouble(SMALL_SIZE, LARGE_SIZE); // Choose the center of the square, allowing for at most half to go out // of the window double x = rgen.nextDouble(-size / 2.0, getWidth() - size / 2.0); double y = rgen.nextDouble(-size / 2.0, getHeight() - size / 2.0); // Create and draw the smart square with the above attributes GCanvas windo = getGCanvas(); GSmartSquare GSR = new GSmartSquare(x, y, size, Color.GREEN, windo); add(GSR); /*********************************************************************** * Add code *here* to change the color of the smart square depending on * if it overflows the window. (Note that the GSmartSquare must be added * to a window before it can know if it is overflowing the window.) ***********************************************************************/ // Pretty up the numeric values before showing in window // (this was before we had the number formatter) double locationx = ((int) (x * 1000)) / 1000.0; double locationy = ((int) (y * 1000)) / 1000.0; double sqSize = ((int) (size * 1000)) / 1000.0; // Add a label showing coordinates and size of current square add(new GLabel("x = " + locationx + " y = " + locationy + " Size is = " + sqSize + " WindowWidth = " + getWidth() + " WindowHeight = " + getHeight()), 10.0, getHeight() - 15.0); // Allow time to observe the results, then erase the window pause(SLEEPTIME); removeAll(); } } // CONSTANT AND GLOBAL OBJECTS DECLARATION SECTION // Square attributes private static final double SMALL_SIZE = 20; private static final double LARGE_SIZE = 350; // number of squares to create private static final int MAX_SQUARES = 20; // pause time for human viewing of results private static final double SLEEPTIME = 1000; // allows random size and location of the squares private RandomGenerator rgen = RandomGenerator.getInstance(); public static void main(String args[]) { (new TestSmartSquare()).start(); } }