    01: import java.awt.BorderLayout;
    02: import java.awt.Color;
    03: import java.awt.GridLayout;
    04: import javax.swing.JFrame;
    05: import javax.swing.JLabel;
    06: import javax.swing.JPanel;
    07: import javax.swing.JSlider;
    08: import javax.swing.event.ChangeListener;
    09: import javax.swing.event.ChangeEvent;
    10:
    11: public class SliderFrame extends JFrame
    12: {
    13:    public SliderFrame()
    14:    {
    15:       colorPanel = new JPanel();
    16:
    17:       add(colorPanel, BorderLayout.CENTER);
    18:       createControlPanel();
    19:       setSampleColor();
    20:       setSize(FRAME_WIDTH, FRAME_HEIGHT);
    21:    }
    22:
    23:    public void createControlPanel()
    24:    {
    25:       class ColorListener implements ChangeListener
    26:       {
    27:          public void stateChanged(ChangeEvent event)
    28:          {
    29:             setSampleColor();
    30:          }
    31:       }
    32:
    33:       ChangeListener listener = new ColorListener();
    34:
    35:       redSlider = new JSlider(0, 100, 100);
    36:       redSlider.addChangeListener(listener);
    37:
    38:       greenSlider = new JSlider(0, 100, 70);
    39:       greenSlider.addChangeListener(listener);
    40:
    41:       blueSlider = new JSlider(0, 100, 70);
    42:       blueSlider.addChangeListener(listener);
    43:
    44:       JPanel controlPanel = new JPanel();
    45:       controlPanel.setLayout(new GridLayout(3, 2));
    46:
    47:       controlPanel.add(new JLabel("Red"));
    48:       controlPanel.add(redSlider);
    49:
    50:       controlPanel.add(new JLabel("Green"));
    51:       controlPanel.add(greenSlider);
    52:
    53:       controlPanel.add(new JLabel("Blue"));
    54:       controlPanel.add(blueSlider);
    55:
    56:       add(controlPanel, BorderLayout.SOUTH);
    57:    }
    58:
    59:    /**
    60:       Reads the slider values and sets the panel to
    61:       the selected color.
    62:    */
    63:    public void setSampleColor()
    64:    {
    65:       // Read slider values
    66:
    67:       float red = 0.01F * redSlider.getValue();
    68:       float green = 0.01F * greenSlider.getValue();
    69:       float blue = 0.01F * blueSlider.getValue();
    70:
    71:       // Set panel background to selected color
    72:
    73:       colorPanel.setBackground(new Color(red, green, blue));
    74:       colorPanel.repaint();
    75:    }
    76:
    77:    private JPanel colorPanel;
    78:    private JSlider redSlider;
    79:    private JSlider greenSlider;
    80:    private JSlider blueSlider;
    81:
    82:    private static final int FRAME_WIDTH = 300;
    83:    private static final int FRAME_HEIGHT = 400;
    84: }