The goal of this example is to write a program that fills the screen with a random collection of debris (GOvals) and then allows the user to click and remove debris pieces. This program lets you practice so many techniques that we learned in the first week of class. You will be using methods, for loops, graphics, random numbers, mouse listeners, and maybe even more!

The first step is to make the debris. Create constants that set limits for how large the debris pieces can be and how many pieces the screen will contain. For every piece, create a new GOval with a random size, position, and color (within the limits you have set). Remember, this should work with any size screen. Perhaps you want to split this into a few methods to make your code easy to read!

Next, we need to let users remove debris from the screen. Add a mouse listener and check if the user has clicked on an object. If they have, clean that debris up (remove it from the screen)!


BONUS!

If you want, as a bonus, you could add a way to check if all the debris is clear. When it is, tell the user congratulations and thank them for cleaning up the screen!

Solution

/**
 * Class: DebrisSweeper
 * -----------------
 * Puts a random collection of debris (GOvals) on the screen
 * and then allows the user to click and remove the debris.
 */
public class DebrisSweeper extends GraphicsProgram {
    /* Minimum and maxmimum size of a piece of debris. */
    private static final double MIN_DEBRIS_SIZE = 50;
    private static final double MAX_DEBRIS_SIZE = 150;
  
    /* Number of pieces of debris. */
    private static final double NUM_DEBRIS_PIECES = 100;
  
    public void run() {
        createDebris();
        addMouseListeners();
    }
  
    public void mousePressed(MouseEvent e) {
        GObject hitObject = getElementAt(e.getX(), e.getY());
        if (hitObject != null) {
          remove(hitObject);
        }
    }
  
    /* * * * * Code for adding debris to the screen. * * * * */
  
    /* Creates debris and strews it randomly on the canvas. */
    private void createDebris() {
        for (int i = 0; i < NUM_DEBRIS_PIECES; i++) {
            createSingleDebrisPiece();
        }
    }
  
    /* Creates a single piece of debris and adds it to the
     * canvas.
     */
    private void createSingleDebrisPiece() {
        RandomGenerator rgen = RandomGenerator.getInstance();
  
        /* Compute the width and height first so that we know where we can
         * safely put the object on screen.
         */
        double width  = rgen.nextDouble(MIN_DEBRIS_SIZE, MAX_DEBRIS_SIZE);
        double height = rgen.nextDouble(MIN_DEBRIS_SIZE, MAX_DEBRIS_SIZE);
        double x = rgen.nextDouble(0, getWidth() - width);
        double y = rgen.nextDouble(0, getHeight() - height);
  
        /* Add an oval of that size and position to the screen. */
        GOval oval = new GOval(x, y, width, height);
        oval.setFilled(true);
        oval.setColor(rgen.nextColor());
        add(oval);
    }
}