import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

/**
 * ColorSelect will demonstrate how to use slider buttons to make
 *  a color mixer that has some beauty, but not enough.
 * 
 * 
 * @author Kathryn McKinley
 * @version April 2007
 * 
 * NOTES: To make the implements ChangeListener work you need to 
 *       import javax.swing.event.*;
 * and to define a method that implements the interface:
 *        public void stateChanged(ChangeEvent e)
 *        
 * 1) Show basics
 * 2) Add tick marks to the slider bars
 * 3) Discuss limitation of GridLayout here, and if time solve it with
 *    additional containers
 */

public class ColorSelect implements ChangeListener{

    // specifies how to layout the parts of the GUI
    // Grid forces all the elements to be the same size
    private static final GridLayout G1LAYOUT_STYLE = new GridLayout(6,1);
    private static final GridLayout G2LAYOUT_STYLE = new GridLayout(1,2);
        
    // Make a window with a title - just one ever
    private JFrame window = new JFrame("Color Mixer");
    
    // Make a painting area to put color in
    private JPanel colorBox = new JPanel();
    
    private JPanel sliderBox = new JPanel();
    
    // Sliders - user selects value in the range of 0 to 100, initial value 0
    // JSlider( int min, int max, int initialValue);
    private JSlider redSlider   = new JSlider(0, 100, 100);
    private JSlider greenSlider = new JSlider(0, 100, 0);
    private JSlider blueSlider  = new JSlider(0, 100, 0);
    
    private JLabel redLabel   = new JLabel ("Red", JLabel.CENTER);
    private JLabel greenLabel = new JLabel ("Green", JLabel.CENTER);     
    private JLabel blueLabel  = new JLabel ("Blue", JLabel.CENTER);
    /**
     * Constructor for objects of class QuestionGUI
     *    configures the GUI
     */
    public ColorSelect()
    {
        window.setVisible(true);
        // register the listner
        redSlider.addChangeListener(this);
        blueSlider.addChangeListener(this);
        greenSlider.addChangeListener(this);
        
        colorBox.setBounds(0, 0, 350, 50);

        // window size and exit 
        window.setSize(500, 700);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
         
        // arrange compoents in GUI
        window.setLayout(G2LAYOUT_STYLE);
        sliderBox.setLayout(G1LAYOUT_STYLE);
        colorBox.setBackground(new Color(0.01F * redSlider.getValue(), 0.01F *greenSlider.getValue(), 0.01F * blueSlider.getValue())); 
        window.add(colorBox);


        addTicks(redSlider, 20);
        addTicks(greenSlider, 50);
        addTicks(blueSlider, 15);
        
        sliderBox.add(redSlider);    
        sliderBox.add(redLabel);
        
        sliderBox.add(greenSlider);    
        sliderBox.add(greenLabel);
        
        sliderBox.add(blueSlider);
        sliderBox.add(blueLabel);
        
        window.add(sliderBox);
        window.pack();
        //window.setVisible(true);
    }
    private void addTicks(JSlider jslider, int ticks) {
        jslider.createStandardLabels(ticks);
        jslider.setPaintLabels(true);
        jslider.setMajorTickSpacing(ticks);
        jslider.setPaintTicks(true);       
    }

    /**
     * This handler is for elements that change, and how to react to them
     * 
     * @param  user event
     */
    public void stateChanged(ChangeEvent e)
    {  
        // Use the response according to the button selection
        float redValue   = 0.01F * redSlider.getValue();
        float greenValue = 0.01F * greenSlider.getValue();
        float blueValue  = 0.01F * blueSlider.getValue();
        
        colorBox.setBackground(new Color(redValue, greenValue, blueValue));
        
        System.out.println(colorBox.getSize());
    }
    public static void main(String[] args) {
        ColorSelect colorSelector = new ColorSelect();   
    }
}
