/* ======================================================================== *
 *    Colors.java - demonstrate JButtons, inner classes, JTextFields        *
 *                                                                          *
 *    Author: Bary W Pollack                                                *
 *    Date:   February 18, 2008                                             *
 * ======================================================================== */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class GUI extends JFrame {
    private JPanel outerPanel;
    private JButton btnStart    = new JButton("Start");
    private JButton btnGuess    = new JButton("Guess");
    private JTextField jtfColor = new JTextField(9);
    private JLabel jlScore      = new JLabel(" ");
    private String[] colorName = {  "BLACK", "BLUE", "CYAN", "DARK_GRAY", "GRAY", 
                                    "GREEN", "LIGHT_GRAY", "MAGENTA", "ORANGE", 
                                    "PINK", "RED", "WHITE", "YELLOW"
                                 };
    private Color[] color =      {  Color.BLACK, Color.BLUE, Color.CYAN, 
                                    Color.DARK_GRAY, Color.GRAY, Color.GREEN, 
                                    Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE, 
                                    Color.PINK, Color.RED, Color.WHITE, Color.YELLOW
                                 };
    private int nColorIndex = 0; // index of the currently displayed color
    private int nCorrect    = 0; // count of number correct guesses
    private int nTries      = 0; // count of total number of trials

    /**
    * Handle all aspects of the GUI
    */
    GUI() {
        super("Colors");
        JPanel jp;

        outerPanel = new JPanel();
        outerPanel.setLayout(new BoxLayout(outerPanel, BoxLayout.Y_AXIS));

        jp = new JPanel();
        jp.add(new JLabel(" "));
        outerPanel.add(jp);

        jp = new JPanel();
        jp.add(new JLabel("JAVA NAMED COLORS"));
        outerPanel.add(jp);

        jp = new JPanel();
        JLabel jlInstr = new JLabel("Click 'Start' to begin;  guess the color "
                                    + "in the box;  then click 'Guess'");
        jp.add(jlInstr);
        outerPanel.add(jp);

        jp = new JPanel();
        btnStart.addActionListener(new StartButtonHandler());
        btnGuess.addActionListener(new GuessButtonHandler());
        btnGuess.setEnabled(false);
        jp.add(btnStart);
        jp.add(new JLabel("    "));
        jp.add(btnGuess);
        outerPanel.add(jp);

        jp = new JPanel();
        jp.add(new JLabel("You think the color is:"));
        jtfColor.setEditable(false);
        jtfColor.addActionListener(new EnterKeyHandler());
        jp.add(jtfColor);
        jp.add(new JLabel("    "));
        jp.add(new ColorPanel());
        outerPanel.add(jp);

        jp = new JPanel();
        jp.add(jlScore);
        outerPanel.add(jp);

        jp = new JPanel();
        jp.add(new JLabel(" "));
        outerPanel.add(jp);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(440, 260);

        setContentPane(outerPanel);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }

    /**
    * Display the color patch
    */
    class ColorPanel extends JPanel {

        public Dimension getPreferredSize() {
            return new Dimension(50, 50); 
        } 

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(color[nColorIndex]);
            g.fillRect(0, 0, 50, 50);
            g.setColor(Color.BLACK);
            g.drawRect(0, 0, 49, 49);
            g.setColor(Color.WHITE);
            g.drawRect(1, 1, 47, 47);
        }

    }

    /**
    * Handle the Start button
    */
    class StartButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            btnGuess.setEnabled(true);
            jtfColor.setEditable(true);
            jtfColor.setText("");
            if (++nColorIndex >= color.length)
                nColorIndex = 0;
            repaint();
            jtfColor.requestFocusInWindow(); 
        }
    }

    /**
    * Semantics of the Guess button and pressing the Enter key
    */
    private void handleGuess() {
        btnGuess.setEnabled(false);
        jtfColor.setEditable(false);
        if (find(jtfColor.getText()) >= 0) {
            ++nCorrect;
            jtfColor.setText("Correct: " + jtfColor.getText());
        } else
            jtfColor.setText("Incorrect: " + jtfColor.getText());
        ++nTries;
        jlScore.setText(String.format("Score:  %d / %d  =  %.1f%%",
                                      nCorrect, nTries, (100.0 * nCorrect) / nTries));
        btnStart.requestFocusInWindow();
    }

    /**
    * Handle the Guess button
    */
    class GuessButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            handleGuess();
        }
     }

    /**
    * Handle the Enter key
    */
    class EnterKeyHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            handleGuess();
        }
    }

    /**
    * Find a color string in the array of color names
    * Return index of color found, or -1 if not found
    */
    private int find(String s) {
        int nLocation = -1;
        s = s.trim();
        for (int i = 0; i < colorName.length; i++)
            if (s.equalsIgnoreCase(colorName[i])) {
                nLocation = i;
                break;
            }
        return nLocation;
    }

}  // end class GUI


/**
* The main class - everything starts here
*/
public class Colors {

    public static void main(String[] args) {
        new GUI();
    }
    
} // end public class Colors
