import objectdraw.*;

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

import javax.swing.*;


/*
 * Class which implements an applet which displays the color selected
 * using three scrollbars.  It illustrates the use of "listeners"
 */
public class ColorMixer extends WindowController implements ActionListener {
    private static final int MIN_COLOR_VALUE = 0; // color range values
    private static final int MAX_COLOR_VALUE = 255;

    // JLabels for color controlled by each of three scrollbars
    private JLabel redJLabel = new JLabel("Red", JLabel.RIGHT);
    private JLabel blueJLabel = new JLabel(" Blue", JLabel.RIGHT);
    private JLabel greenJLabel = new JLabel("Green", JLabel.RIGHT);

    // 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
        selectorPanel.add(redJLabel);
        selectorPanel.add(redValueField);

        selectorPanel.add(blueJLabel);
        selectorPanel.add(blueValueField);

        selectorPanel.add(greenJLabel);
        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);

        // Add panel to content pane of window
        Container contentPane = getContentPane();
        contentPane.add(selectorPanel, BorderLayout.SOUTH);
        contentPane.validate();

        // 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 stringValue = field.getText();
        int mix = Integer.parseInt(stringValue);

        if (mix < MIN_COLOR_VALUE) {
            mix = MIN_COLOR_VALUE;
            field.setText("" + MIN_COLOR_VALUE);
        } else if (mix > MAX_COLOR_VALUE) {
            mix = MAX_COLOR_VALUE;
            field.setText("" + MAX_COLOR_VALUE);
        }

        return mix;
    }

    /*
     * 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);
    }
}
