import objectdraw.*; import java.awt.*; import java.util.Random; // A program to check the random color of a square public class ColorCompare extends FrameWindowController { private static final Location RECT_LOC = new Location(100, 100); private static final int RECT_SIZE = 50; private static final Location TEXT_LOC = new Location(100, 50); private static final int TEXT_SIZE = 20; private static final int NUM_COLORS = 3; private Random rand = new Random(); private FilledRect square; private Text display; public void begin() { square = new FilledRect(RECT_LOC, RECT_SIZE, RECT_SIZE, canvas); display = new Text("Click to see if it's purple!", TEXT_LOC, canvas); display.setFontSize(TEXT_SIZE); } public void onMouseMove(Location point) { int color = rand.nextInt(NUM_COLORS); if (color == 0) { // set to grey square.setColor(new Color(100, 100, 100)); } else if (color == 1) { // set to light blue square.setColor(new Color(0, 255, 255)); } else { // set to purple square.setColor(new Color(255, 0, 255)); } display.setText("Click to see if it's purple!"); } public void onMouseClick(Location point) { Color testColor = new Color(255, 0, 255); if (square.getColor().equals(testColor)) { display.setText("Yes, it's purple!"); } else { display.setText("Nope, not purple!"); } } }