/** * Creates a frame and draws some random circles in it * @author harrchen * @version 1.0 * Last Modified 04/24/02 */ import java.awt.*; import java.awt.event.*; import java.util.*; public class DrawCircles extends Frame { // We declare some constants here (the size of the frame) public static final int height=500; public static final int width=500; // These are the "Draw" and "Quit" buttons protected Button draw; protected Button quit; // This is for our random number generation protected Random randomGen; /** * This class listens to triggers of the "Draw" button */ protected class DrawButtonListener implements ActionListener { /** * Tells the frame to repaint on an action event * @param e The ActionEvent corresponding to the action */ public void actionPerformed(ActionEvent e) { repaint(); } } /** * This class listens to triggers of the "Quit" button */ protected class QuitButtonListener implements ActionListener { /** * Terminates the application on an action event * @param e The ActionEvent corresponding to the action */ public void actionPerformed(ActionEvent e) { System.exit(0); } } /** * main function - creates the DrawCircles frame * @param args Command line arguments */ public static void main(String[] args) { // Create a DrawCircles frame and display it DrawCircles drawCircles = new DrawCircles(); drawCircles.show(); } /** * Creates and initializes a DrawCircles fram with some defaults */ protected DrawCircles() { // Initialize the frame's details setSize(height,width); setTitle("DrawCircles Application"); setLayout(new FlowLayout()); // We create the buttons and make event handlers for them draw=new Button("Draw"); quit=new Button("Quit"); draw.addActionListener(new DrawButtonListener()); quit.addActionListener(new QuitButtonListener()); add(draw); add(quit); // Create the random number generator randomGen=new Random(); } /** * Returns the number of circles that should be drawn */ public int numberOfCircles() { // Return a default 20 for now return 20; } /** * Returns the minimum diameter of a circle to draw */ public int minCircleDiameter() { // Return a default 10 for now return 10; } /** * Returns the maximum diameter of a circle to draw */ public int maxCircleDiameter() { // Return a default 30 for now return 30; } /** * Returns the color of a circle to draw */ public Color circleColor() { // Returns a color with random RGB components return new Color(randomGen.nextFloat(), randomGen.nextFloat(), randomGen.nextFloat()); } /** * Does the actual drawing of the circles */ public void paint(Graphics g) { int diameter; // Store locally the range of diameters for code readability int range=maxCircleDiameter()-minCircleDiameter(); // This clears the window g.clearRect(0,0,width,height); // This draws the circles for (int i=0; i