import java.awt.*; import java.applet.*; /** * A simple square class. * * @author Samuel A. Rebelsky * @version 1.1 of February 1999 */ public class Square { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** The current color of the square. */ protected Color color; /** The current edge size of the square. */ protected int size; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ /** * Create a new square with specified edge size and color. */ public Square(int size, Color color) { this.size = size; this.setColor(color); } // Square(int, Color) /** * Create a new square with specified edge size and default color. */ public Square(int size) { this.size = size; this.setColor(Color.blue); // Blue is as good a default as any. } // Square(int) // +---------+------------------------------------------------- // | Methods | // +---------+ /** * Draw this square centered at a particular position in a * graphics thingy. To make it look more interesting, I outline * the square in some color. */ public void draw(int x, int y, Graphics g) { g.setColor(this.color); g.fillRect( x-this.size/2, // Left margin y-this.size/2, // Top margin this.size, // Width this.size); // Height } // draw(int,int,Graphics) /** * Get the size of this square. */ public int getSize() { return this.size; } // getSize() /** * Set the color of this square. */ public void setColor(Color newColor) { this.color = newColor; } // setColor(Color) /** * Set the size of this square. */ public void setSize(int newSize) { this.size = newSize; } // setSize(int) } // class Square