Button5Debug (rest of program is same as Button5)

File: java/Button5Debug/Application.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

/**
 * The "application" (vs. user interface) part of this program
 */
public class Application {
    /** Application data */
    private ArrayList<Square> squares = new ArrayList<Square> ();
    private boolean drawX = false;

    private MyCanvas canvas;
    private Square current;

    public Application (int n, MyCanvas canvas) {
	// Stash ivar
	this.canvas = canvas;

	// Create the n objects
	for (int i=0; i<n; i++) {
	    squares.add (new Square(canvas));
	}

	// Initialize to some arbitrary square so current is never null
	Square current = squares.get(0);

	// assert => we expect this always to be true
	assert current != null;
    }

    public void drawApplication (Graphics g) {
	// The background X
	if (drawX) {
	    Dimension size = canvas.getSize();
	    g.drawLine (0, 0, size.width, size.height);
	    g.drawLine (0, size.height, size.width, 0);
	}

	// Enumerate our list
	for (Square s: squares) {
	    // N.B. (s==current) is a boolean expr,
	    // evaluates to true or false
	    s.draw (g, s==current);
	}

	assert current != null;
    }

    public void setX (boolean newX) {
	drawX = newX;
	canvas.repaint();

	assert current != null;
    }

    Square getCurrent () {
	assert current != null;
	return current;
    }

    public void doMouse (Point p) {
	for (Square s: squares) {
	    if (s.isInside(p)) {
		current = s;
		break;
	    }
	}
	
	canvas.repaint();

	assert current != null;
    }
}

File: java/Button5Debug/Square.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

/**
 * Holds part of the Application data
 */
public class Square {
    /** Classvar to generate unique positions for each instance */
    static private int squareCount = 0;

    private MyCanvas canvas;

    /** Info about the square */
    private Rectangle loc;

    /**
     * Standalone unit test for Square class.
     * Not called in normal usage,
     * run it separately with "java Square"
     */
    public static void main (String[] args) {
	// Need a dummy object here for testing
	MyCanvas canvas = new MyCanvas ();

	// Create some squares
	ArrayList<Square> squares = new ArrayList<Square> ();
	for (int i=0; i<10; i++) {
	    squares.add (new Square(canvas));
	}

	// Print them out
	for (Square s: squares) {
	    System.out.println ("Square at " + s.loc);
	    if (s.isInside (new Point (50, 50))) {
		System.out.println ("is inside");
	    }
	}

	// Test a little
	squares.get(0).bigger();
	squares.get(1).right();
	squares.get(2).left();
	squares.get(2).smaller();

	for (Square s: squares) {
	    System.out.println ("Square at " + s.loc);
	    if (s.isInside (new Point (50, 50))) {
		System.out.println ("is inside");
	    }
	}
    }
    
    public Square (MyCanvas canvas) {
	this.canvas = canvas;

	// Keep count
	squareCount++;

	// Initial values
	loc = new Rectangle (20*squareCount, 10*squareCount, 50, 50);
    }

    public void draw (Graphics g, boolean isCurrent) {
	Graphics2D g2 = (Graphics2D) g;

	if (isCurrent)g2.fill (loc);
	else g2.draw (loc);
    }

    public void left () {
	loc.x -= 10;
	canvas.repaint();
    }

    public void right () {
	loc.x += 10;
	canvas.repaint();
    }

    public void bigger () {
	loc.width += 10;
	loc.height += 10;
	canvas.repaint();
    }

    public void smaller () {
	loc.width -= 10;
	loc.height -= 10;
	canvas.repaint();
    }

    public boolean isInside (Point p) {
	return loc.contains (p);
    }
}