// file: TestCircle.java // date: 3/23/2014 // authors: Andrew Mertz and Nancy Van Cleave // // This program should be used to test your GCircle class before continuing. // A GCircle, pocket, is created in the center of the window, then for MAX_MOVES // steps another GCircle is randomly resized and moved around the window. If the // center of this circle lands inside the pocket, it will appear red, else it will // appear green. package testing; import acm.graphics.*; import acm.program.*; import acm.util.*; import java.awt.*; import graphics.*; public class TestCircle extends GraphicsProgram { public void init() { setSize(600, 600); } public void run() { // Create the a pocket in the center of the window with radius equal to 25% // of the width of the window. double r = 0.25 * getWidth(); GCircle pocket = new GCircle(getWidth()/2.0 - r, getHeight()/2.0 - r, 2*r, Color.BLACK); add(pocket); // Add an "outline" of a circle and its center point to the window. For now // we do not care where they go as we will be moving them in a moment. GCircle outline = new GCircle(0,0,0); add(outline); GCircle point = new GCircle(0,0,POINT_SIZE); point.setFilled(true); add(point); // Take MAX_MOVE steps, resizing and moving the circle. If its center falls // inside the pocket it should be red, otherwise (center is outside the pocket), // it should be green for (int i = 1; i <= MAX_MOVES; i++) { // Choose the size (diameter) of the outline randomly double size = rgen.nextDouble(SMALL_SIZE, LARGE_SIZE); // Choose the center of the circle double x = rgen.nextDouble(0, getWidth()); double y = rgen.nextDouble(0, getHeight()); // Move and resize the circles outline.setBounds(x - size/2.0, y - size/2.0, size); point.setLocation(x - POINT_SIZE/2.0, y - POINT_SIZE/2.0); // If we are inside the pocket, color the circle red. if(pocket.captured(outline)) { outline.setColor(Color.RED); } else // Otherwise color it green. { outline.setColor(Color.GREEN); } // Allow time to observe the results, then erase the window pause(SLEEPTIME); } } // CONSTANT AND GLOBAL OBJECTS DECLARATION SECTION // Circle attributes private static final double POINT_SIZE = 5; private static final double SMALL_SIZE = 20; private static final double LARGE_SIZE = 350; // number of times to move/relocate the circle, and pause time for animation private static final int MAX_MOVES = 20; private static final double SLEEPTIME = 1000; // allows for random location and size of circle private RandomGenerator rgen = RandomGenerator.getInstance(); public static void main(String args[]) { (new TestCircle()).start(); } }