UNB/ CS/ David Bremner/ teaching/ java/ SliderTest.java
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

public class SliderTest
{  public static void main(String[] args)
   {  SliderFrame frame = new SliderFrame();
      frame.setTitle("SliderTest");
      frame.show();
   }
}

class SliderFrame extends JFrame
{  public SliderFrame()
   {  final int DEFAULT_FRAME_WIDTH = 300;
      final int DEFAULT_FRAME_HEIGHT = 300;
      setSize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT);

      addWindowListener(new WindowCloser());
      
      // construct components

      colorPanel = new JPanel();

      ColorListener listener = new ColorListener();

      redSlider = new JSlider(0, 100, 100);
      redSlider.addChangeListener(listener);

      greenSlider = new JSlider(0, 100, 70);
      greenSlider.addChangeListener(listener);

      blueSlider = new JSlider(0, 100, 70);
      blueSlider.addChangeListener(listener);
      
      // fill content pane

      JPanel southPanel = new JPanel();
      southPanel.setLayout(new GridLayout(3, 2));
      southPanel.add(new JLabel("Red", 
         SwingConstants.RIGHT));
      southPanel.add(redSlider);
      southPanel.add(new JLabel("Green", 
         SwingConstants.RIGHT));
      southPanel.add(greenSlider);
      southPanel.add(new JLabel("Blue", 
         SwingConstants.RIGHT));
      southPanel.add(blueSlider);

      Container contentPane = getContentPane();
      contentPane.add(colorPanel, "Center");
      contentPane.add(southPanel, "South");

      setSampleColor();
   }
   
   /**
      Reads the slider values and sets the panel to
      the selected color.
   */
   public void setSampleColor()
   {  // read slider values
   
      float red = 0.01F * redSlider.getValue();
      float green = 0.01F * greenSlider.getValue();
      float blue = 0.01F * blueSlider.getValue();

      // set panel background to selected color
      
      colorPanel.setBackground(new Color(red, green, blue));
      colorPanel.repaint();
   }
   
   private JPanel colorPanel;
   private JSlider redSlider;
   private JSlider greenSlider;
   private JSlider blueSlider;

   private class ColorListener implements ChangeListener
   {  public void stateChanged(ChangeEvent event)
      {  setSampleColor();
      }
   }   

   private class WindowCloser extends WindowAdapter
   {  public void windowClosing(WindowEvent event)
      {  System.exit(0);
      }
   }
}