import java.awt.*; import java.applet.*; /** * A simple circle class with a little bit of extra * facilities. * * @author Samuel A. Rebelsky * @version 1.0 of February 1999 */ public class NewCircle { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** The current color of the circle. */ protected Color color; /** * The ``opposite'' of the current color. Used for outlining * the circle. This is not part of the assignment, but was done * for ``extra credit'' as it were. */ protected Color opposite; /** The current diameter of the circle. */ protected int diameter; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ /** * Create a new circle with specified diameter and color. */ public NewCircle(int diameter, Color color) { this.diameter = diameter; this.setColor(color); } // NewCircle(int, Color) /** * Create a new circle with specified diameter and default color. */ public NewCircle(int diameter) { this.diameter = diameter; this.setColor(Color.blue); // Blue is as good a default as any. } // NewCircle(int) // +---------+------------------------------------------------- // | Methods | // +---------+ /** * Draw this circle centered at a particular position in a * graphics thingy. To make it look more interesting, I outline * the circle in some color. */ public void draw(int x, int y, Graphics g) { g.setColor(this.color); g.fillOval( x-this.diameter/2, // Left margin y-this.diameter/2, // Top margin this.diameter, // Width this.diameter); // Height g.setColor(this.opposite); g.drawOval( x-this.diameter/2, // Left margin y-this.diameter/2, // Top margin this.diameter, // Width this.diameter); // Height } // draw(int,int,Graphics) /** * Get the diameter of this circle. */ public int getSize() { return this.diameter; } // getSize() /** * Set the color of this circle. */ public void setColor(Color newColor) { this.color = newColor; // The opposite has RGB ``rotated'' by 128. Not quite // opposite, but perhaps good enough for most purposes // (and nice in that the opposite of (128,128,128) is not // (127,127,127)). this.opposite = new Color((this.color.getRed() + 128) % 256, (this.color.getGreen() + 128) % 256, (this.color.getBlue() + 128) % 256); } // setColor(Color) /** * Set the diameter of this circle. */ public void setSize(int newDiameter) { this.diameter = newDiameter; } // setSize(int) } // class NewCircle