1	//  File:  AutoPolygons - Automatically draw random Polygons in random colors
     2	//         Demonstrate Polygon creation
     3	//         Demonstrate Random and Math.random()
     4	//         Demonstrate use of a Timer
     5	//         Demonstrate resizing an Applet
     6	//         Based on RanPolygons

     7	import java.awt.*;
     8	import java.awt.event.*;
     9	import java.applet.*;
    10	import java.util.Random;
    11	import javax.swing.Timer;

    12	public class AutoPolygons extends Applet {

    13	    /**
    14	    * Initialize the background; set up the Start/Stop button
    15	    */
    16	    public void init() {
    17	        setBackground(Color.green.darker());
    18	        startButton.addActionListener(new StartButtonHandler());
    19	        add(startButton);
    20	        refreshButton.addActionListener(new RefreshButtonHandler());
    21	        add(refreshButton);
    22	        refreshButton.setEnabled(false);
    23	        delayButton.addActionListener(new DelayButtonHandler());
    24	        add(delayButton);
    25	        timer = new Timer(nDelay, new TimerHandler());
    26	    }

    27	    /**
    28	    * (Re-) paint the screen
    29	    */
    30	    public void paint(Graphics g) {
    31	        color = newColor();
    32	        displayPolygon(g);
    33	    }

    34	    // Create a random Color
    35	    private Color newColor() {
    36	        return new Color(ran256(), ran256(), ran256());
    37	    }

    38	    /**
    39	    * Display a randomly colored Polygon, with a black outline
    40	    */
    41	    private void displayPolygon(Graphics g) {
    42	        if (polygon != null) {
    43	            g.setColor(color);
    44	            g.fillPolygon(polygon);
    45	            g.setColor(Color.black);
    46	            g.drawPolygon(polygon);
    47	        }
    48	    }

    49	    /* Generate an int in the range [0..255] */
    50	    private int ran256() { return (int) (256 * Math.random()); }

    51	    /**
    52	    * Create and return an nNumPoints Polygon, in the 2D area [nMin, nMin]
    53	    */
    54	    private Polygon createPoly(final int nNumPoints, final int nMin) {
    55	        Polygon polygon = new Polygon();
    56	        for (int i = 0; i < nNumPoints; i++) {
    57	            int x = random.nextInt(nMin);
    58	            int y = random.nextInt(nMin);
    59	            polygon.addPoint(x, y);
    60	        }
    61	        return polygon;
    62	    }

    63	    /**
    64	    * Semantics of the "Start/Stop" button
    65	    */
    66	    private class StartButtonHandler implements ActionListener {
    67	        public void actionPerformed(ActionEvent e) {
    68	            if (timer.isRunning()) {
    69	                // Semantics for 'Stop'
    70	                startButton.setLabel("      Start      ");
    71	                refreshButton.setEnabled(true);
    72	                nDelay /= 2;
    73	                timer.stop();
    74	                statusMessage = " Press 'Start' to begin drawing...";
    75	            } else {
    76	                // Semantics for 'Start'
    77	                startButton.setLabel("       Stop       ");
    78	                refreshButton.setEnabled(false);
    79	                timer.setDelay(nDelay);
    80	                timer.start();
    81	                statusMessage = " Press 'Stop' to stop drawing...  ";
    82	                dimension = getSize();
    83	                if (dimension.width <= 1010)
    84	                    dimension.width  += 20;
    85	                if (dimension.height <= 640)
    86	                    dimension.height += 20;
    87	                setSize(dimension.width, dimension.height);
    88	            }
    89	            showStatus(statusMessage + "    Delay=" + nDelay + "ms");
    90	        }
    91	    }

    92	    /**
    93	    * Semantics of the "Refresh" button
    94	    */
    95	    private class RefreshButtonHandler implements ActionListener {
    96	        public void actionPerformed(ActionEvent e) {
    97	            repaint();
    98	        }
    99	    }

   100	    /**
   101	    * Semantics of the "Reset Delay" button
   102	    */
   103	    private class DelayButtonHandler implements ActionListener {
   104	        public void actionPerformed(ActionEvent e) {
   105	            nDelay = DELAY;
   106	            timer.setDelay(nDelay);
   107	            showStatus(statusMessage + "    Delay=" + nDelay + "ms");
   108	        }
   109	    }

   110	    /**
   111	    * Semantics of the Timer
   112	    */
   113	    private class TimerHandler implements ActionListener {
   114	        public void actionPerformed(ActionEvent e) {
   115	            nNumPoints = random.nextInt(8) + 3;
   116	            int nMin = Math.min(getSize().width, getSize().height);
   117	            polygon = createPoly(nNumPoints, nMin);
   118	            repaint();
   119	        }
   120	    }

   121	    private int nNumPoints;         // Number of points in the Polygon
   122	    private Color color;            // The Color handle
   123	    private Polygon polygon;        // The Polygon handle
   124	    private Button startButton   = new Button("      Start      ");
   125	    private Button refreshButton = new Button("    Refresh    ");
   126	    private Button delayButton   = new Button(" Reset Delay ");
   127	    private String statusMessage = "Press 'Start' to start drawing...";
   128	    private Random random = new Random(1234L);
   129	    private final int DELAY = 256;
   130	    private int nDelay = DELAY;
   131	    private Timer timer;
   132	    private Dimension dimension;
   133	}