import objectdraw.*;

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

import javax.swing.*;


/**
 * Color Mixer that uses Exceptions to catch errors.
 */
public class ColorMixer extends WindowController implements ActionListener {
    // Labels 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 labels on panels
    public void begin() {
        // We want the labels and text fields to be next to each other.
        // So use a GridLayout that gives us:
        // 3 rows, one for each color
        // 2 columns, one for the label, one for the text field
        JPanel selectorPanel = new JPanel();
        selectorPanel.setLayout(new GridLayout(3, 2));

        // Set up the labels and text fields in selectorPanel
        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);

       Container contentPane = getContentPane();
       contentPane.add(selectorPanel, "South");

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

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

        contentPane.validate();
    }

    /*
     * 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) {
        try {
            // get component color values
            int red = Integer.parseInt(redValueField.getText());
            int green = Integer.parseInt(greenValueField.getText());
            int blue = Integer.parseInt(blueValueField.getText());

            // Create the new color and make it the color for colorRect
            Color newColor = new Color(red, green, blue);
            colorRect.setColor(newColor);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Bad Color Input");
        }

    }
}
