This example lets you treat your computer like a piece of paper and your mouse like a hole puncher. The user should be able to click anywhere on the window and create holes (GOvals).

Make a constant that describes the radius of the holes you will punch. 10 is a good size for this. Remember that GOvals want to see a double for this variable (not an int). Then add a mouse listener and create a draw method to react with every time the user punches a new hole.

Solution

/**
 * Class: HolePuncher
 * -----------------
 * A program that draws filled black circles on the screen whenever
 * the mouse
 * is pressed.
 */
public class HolePuncher extends GraphicsProgram {
  /* The radius of each hole that we punch. */
  private static final double HOLE_RADIUS = 10;

  public void run() {
    addMouseListeners();
  }

  /**
   * Draws a black circle centered at the mouse location whenever the mouse
   * is pressed.
   */
  public void mousePressed(MouseEvent e) {
    double x = e.getX() - HOLE_RADIUS;
    double y = e.getY() - HOLE_RADIUS;

    GOval hole = new GOval(x, y, 2 * HOLE_RADIUS, 2 * HOLE_RADIUS);
    hole.setFilled(true);
    add(hole);
  }
}