MysteryProgram


Your browser is ignoring the <APPLET> tag!

This program draws a black filled oval when the program starts. As the user moves the mouse, the oval changes to a random color. When the user clicks the mouse, a new oval is drawn at a random location. The new oval will be just a bit redder than the previous oval. If it cannot get any redder, the new oval will have a random color.

import objectdraw.*;
import java.awt.*;

public class MysteryProgram extends WindowController
{
    private static final int SIZE = 50;
    private static final int LEFT = 100;
    private static final int TOP = 100;
    
    private static final int WINDOW_SIZE = 400;
    
    private FilledOval shape;
    private RandomIntGenerator colorGenerator = new RandomIntGenerator (0, 255);
    private RandomIntGenerator locationGenerator = new RandomIntGenerator (0, WINDOW_SIZE - SIZE);
    
    public void begin () {
        resize (WINDOW_SIZE, WINDOW_SIZE);
        shape = new FilledOval (LEFT, TOP, SIZE, SIZE, canvas);
    }
    
    public void onMouseMove (Location point) {
        shape.setColor (new Color (colorGenerator.nextValue(), 
                                   colorGenerator.nextValue(), 
                                   colorGenerator.nextValue()));
    }
    
    public void onMouseClick (Location point) {
        Color shapeColor = shape.getColor();
        int redness = shapeColor.getRed();
        int greenness = shapeColor.getGreen();
        int blueness = shapeColor.getBlue();
        shape = new FilledOval (locationGenerator.nextValue(), locationGenerator.nextValue(), 
                SIZE, SIZE, canvas);
        if (redness <= 245) {
            redness = redness + 10;
            shapeColor = new Color (redness, greenness, blueness);
            shape.setColor (shapeColor);
        }
        else {
            shape.setColor (new Color (colorGenerator.nextValue(), 
                                   colorGenerator.nextValue(), 
                                   colorGenerator.nextValue()));
        }        
    }
}