//  File:  RanPolygons - Draw random Polygons in random colors
//         Demonstrate Polygon creation
//         Demonstrate Random and Math.random()

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.Random;

public class RanPolygons extends Applet {

    /**
     *  Initialize the background; set up the two buttons
     */
    public void init() {
        setBackground(Color.green.darker());
        polyButton.addActionListener(new PolyButtonHandler());
        add(polyButton);
        repaintButton.addActionListener(new RepaintButtonHandler());
        add(repaintButton);
        repaintButton.setEnabled(false);
    }

    /**
     *  (Re-) paint the screen
     */
    public void paint(Graphics g) {
        color = newColor();
        displayPolygon(g);
        drawLabel(g, nNumPoints);
        showStatus(" You should resize this applet several times");
    }

    /**
     *  Display feedback label, with black-shadowed text
     */
    private void drawLabel(Graphics g, final int nNumPoints) {
        if (nNumPoints > 0) {
            g.setColor(Color.black);
            g.drawString("Random Polygons", 11, 21);
            g.drawString(nNumPoints + " points", 11, 41);
            g.drawString("(" + color.getRed() + "," + color.getGreen() + "," 
                         + color.getBlue() + ")", 11, 61);
            g.setColor(Color.white);
            g.drawString("Random Polygons", 10, 20);
            g.drawString(nNumPoints + " points", 10, 40);
            g.drawString("(" + color.getRed() + "," + color.getGreen() + ","
                         + color.getBlue() + ")", 10, 60);
        }
    }

    //  Create a random Color
    private Color newColor() {
        return new Color(ran256(), ran256(), ran256());
    }

    /**
     *  Display a randomly colored Polygon, with a black outline
     */
    private void displayPolygon(Graphics g) {
        if (polygon != null) {
            g.setColor(color);
            g.fillPolygon(polygon);
            g.setColor(Color.black);
            g.drawPolygon(polygon);
        }
    }

    /*  Generate an int in the range [0..255] */
    private int ran256() { return (int) (256 * Math.random()); }

    /**
     *  Create and return an nNumPoints Polygon, in the 2D area [nMin, nMin]
     */
    private Polygon createPoly(final int nNumPoints, final int nMin) {
        Polygon polygon = new Polygon();
        for (int i = 0; i < nNumPoints; i++) {
            int x = random.nextInt(nMin);
            int y = random.nextInt(nMin);
            polygon.addPoint(x, y);
        }
        return polygon;
    }

    /**
     *  Semantics of the "New Poly" button:  Create a new Polygon
     *  and, enable the "Repaint" button
     */
    private class PolyButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            nNumPoints = random.nextInt(8) + 3;
            int nMin = Math.min(getSize().width, getSize().height);
            polygon = createPoly(nNumPoints, nMin);
            repaintButton.setEnabled(true);
            repaint();
        }
    }

    /**
     *  Semantics of the "Repaint" button:  Request a screen refresh
     */
    private class RepaintButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            repaint();
        }
    }
    
    private int nNumPoints;         // Number of points in the Polygon
    private Color color;            // The Color handle
    private Polygon polygon;        // The Polygon handle
    private Button polyButton    = new Button("  New Poly  ");
    private Button repaintButton = new Button("  Repaint  ");
    private Random random = new Random(1234L);

}
