ButtonApp4: Square.java

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

/**
 * Holds part of the Model data
 */
public class Square {
    /** Classvar to generate unique positions for each instance */
    static private int squareCount = 0; //*1 Keep count in classvar

    /** Enumerated type for which action to take on a square */
    public enum ButtonAction {LEFT, RIGHT, BIGGER, SMALLER};

    private Canvas canvas;

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

    public Square (Canvas canvas) {
	this.canvas = canvas;

	// Keep count
	squareCount++; //*1

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

    public void draw (Graphics g, boolean isCurrent) { //*3 Draw based on isCurrent param
	Graphics2D g2 = (Graphics2D) g;

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

    public boolean isInside (Point p) { //*2 Preferable to manual formula
	return loc.contains (p); //*2
    }

    public void doAction (ButtonAction action) {
	if (action==ButtonAction.LEFT) loc.x -= 10;
	else if (action==ButtonAction.RIGHT) loc.x += 10;
	else if (action==ButtonAction.BIGGER) { loc.width += 10; loc.height += 10; }
	else if (action==ButtonAction.SMALLER) { loc.width -= 10; loc.height -= 10; }

	canvas.repaint();
    }
}

[download file]