import objectdraw.*;

import java.awt.*;
import java.awt.event.*;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


/**
  * Color Mixer that uses text fields.  This
  * illustrates how to check for valid numeric
  * characters in the text fields.
  */
public class ColorMixer extends WindowController implements ActionListener {

    // JLabels to display current value of each scrollbar
    private JTextField redValueField = new JTextField("0", 3);
    private JTextField greenValueField = new JTextField("0", 3);
    private JTextField blueValueField = new JTextField("0", 3);
    private FilledRect colorRect; // Rectangle displaying chosen color

    // Set up Scrollbars and JLabels on panels
    public void begin() {
        // Set up panel to hold three text fields and their JLabels
        JPanel selectorPanel = new JPanel();

        // We want the JLabels and text fields to be next to each other, so use a GridLayout
        // This gives us:
        //    3 rows, one for each color
        //    2 columns, one for the JLabel, one for the text field
        //    10 pixels between the JLabel and text field
        //    5 pixels beween rows
        selectorPanel.setLayout(new GridLayout(3, 2, 10, 5));

        // Set up the JLabels and text fields
        JLabel redLabel = new JLabel("Red", JLabel.RIGHT);
        JLabel blueLabel = new JLabel(" Blue", JLabel.RIGHT);
        JLabel greenLabel = new JLabel("Green", JLabel.RIGHT);

        selectorPanel.add(redLabel);
        selectorPanel.add(redValueField);
        selectorPanel.add(blueLabel);
        selectorPanel.add(blueValueField);
        selectorPanel.add(greenLabel);
        selectorPanel.add(greenValueField);

        // Add listeners to the fields so we know when the user changes the value.
        redValueField.addActionListener(this);
        blueValueField.addActionListener(this);
        greenValueField.addActionListener(this);

        getContentPane().add(selectorPanel, BorderLayout.SOUTH); // Add selectorPanel to south of canvas

        // create color display
        colorRect = new FilledRect(0, 0, canvas.getWidth(), canvas.getHeight(),
                canvas);
    }

    /*
     * Get the value out of a text field and convert it to an integer.
     * If the value is below the minimum allowed color value, set it
     * to the minimum and update the text field.  If it is above the
     * maximum, set it to the maximum and update the text field.
     */
    private int getColorMix(JTextField field) {
        String fieldText = field.getText();

        if (!isInteger(fieldText)) {
            field.setText("0");
            return 0;
        }

        int mix = Integer.parseInt(fieldText);

        if (mix < 0) {
            mix = 0;
            field.setText("0");
        } else if (mix > 255) {
            mix = 255;
            field.setText("255");
        }

        return mix;
    }

    /*
     * Checks whether a given string can be interpreted as an integer:
     * i.e., checks that it is made up of characters that are digits.
     */
    private boolean isInteger(String text) {
        for (int i = 0; i < text.length(); i++) {
            char ch = text.charAt(i);
            if (ch < '0' || ch > '9') {
                return false;
            }
        }

        return true;
    }

    /*
     * This method is called whenever the user hits carriage return
     * in a text field.  It repaints the colorRect with the color
     * obtained from the text field values.
     */
    public void actionPerformed(ActionEvent evt) {
        // get component color values
        int redValue = getColorMix(redValueField);
        int greenValue = getColorMix(greenValueField);
        int blueValue = getColorMix(blueValueField);

        // Create the new color and make it the color for colorRect
        Color newColor = new Color(redValue, greenValue, blueValue);
        colorRect.setColor(newColor);
    }
}
