import java.awt.*;
/**
* Square holds the "model" data for one square
*/
public class Square {
/** Enumerated type for which action to take */
public enum ButtonAction {LEFT, RIGHT, BIGGER, SMALLER, FILL, EMPTY}; //*5 Java enumerated type
/** The model data */
private Rectangle loc = new Rectangle (100, 50, 50, 50); //*1 The model data
private boolean filled = false; //*1
private Canvas canvas;
public Square (Canvas canvas) {
this.canvas = canvas;
}
public void draw (Graphics g) { //*2 Draw the square
Graphics2D g2 = (Graphics2D) g;
if (filled) g2.fill (loc); //*2
else g2.draw (loc); //*2
}
public void doAction (ButtonAction action) { //*3 Handle actions
if (action==ButtonAction.LEFT) loc.x -= 10; //*3
else if (action==ButtonAction.RIGHT) loc.x += 10; //*3
else if (action==ButtonAction.BIGGER) { loc.width += 10; loc.height += 10; } //*3
else if (action==ButtonAction.SMALLER) { loc.width -= 10; loc.height -= 10; } //*3
else if (action==ButtonAction.FILL) filled = true; //*3
else if (action==ButtonAction.EMPTY) filled = false; //*3
canvas.repaint(); //*4 Need full repaint in case action exposed the other square
}
}