import java.awt.*; import rebelsky.io.SimpleGUI; /** * Draw a set of movable concentric circles on the screen. *
* Copyright (c) 1998 Samuel A. Rebelsky. All rights reserved. * * @author Samuel A. Rebelsky * @version 1.1 of February 1998 */ public class Circles extends SimpleGUI { /* * Implementation notes: We'll keep track of the center of the * circles and the radius of the innermost circle. When the * user presses a specified key, we'll change the radius or center. */ // +-----------+--------------------------------------------------------- // | Constants | // +-----------+ /** * The base change in diameter when the user shrinks or enlarges * the circle. In actuality, the circle may change by more, as we * work with circles with different diameters. */ protected static int DELTA_SIZE = 1; /** * The change in X or Y position. An appropriate number was determined * by experimentation. */ protected static int DELTA_POS = 3; // +--------+------------------------------------------------------------ // | Fields | // +--------+ /** * The basic radius of circles. */ protected int radius; /** * The x position (distance from left margin) of the center of * the circles. */ protected int x; /** * The y position (distance from top margin) of the center of the * the circles. */ protected int y; // +--------------+------------------------------------------------------ // | Constructors | // +--------------+ /** * Start with the circle of radius 10, centered at (100,100). */ public Circles() { radius = 10; x = 100; y = 100; } // Circles // +---------+----------------------------------------------------------- // | Methods | // +---------+ /** * Handle key presses using the numeric keypad to govern changes. * 4 is left, 8 is up, and so on and so forth. Instructions are * given to the user in the title. */ public void handleKey(char key) { switch (key) { case '0': // Enlarge radius = radius + DELTA_SIZE; break; case '2': // Down y = y + DELTA_POS; break; case '4': // Left x = x - DELTA_POS; break; case '5': // Reduce radius = radius - DELTA_SIZE; break; case '6': // Right x = x + DELTA_POS; break; case '8': // Up y = y - DELTA_POS; break; default: super.handleKey(key); break; } // switch } // handleKey /** * Paint the circles on the screen. */ public void paintStuff(Graphics g) { // Outermost circle: red, 3*base size g.setColor(Color.red); g.drawOval(x-radius*3, y-radius*3, radius*6, radius*6); // Next circle: blue, 2*base size g.setColor(Color.blue); g.drawOval(x-radius*2, y-radius*2, radius*4, radius*4); // Next circle: green, 1*base size g.setColor(Color.green); g.drawOval(x-radius*1, y-radius*1, radius*2, radius*2); } // paintStuff(Graphics) // +------+-------------------------------------------------------------- // | Main | // +------+ /** * Set the title and then start running the GUI. */ public static void main(String[] args) { Circles gui = new Circles(); gui.setTitle("Circles"); gui.setCaption("4-left; 2-down; 8-up; 6-right; 5-shrink; 0-enlarge; q-quit"); gui.run(); } // main(String[]) } // Circles