/* This program demonstrates the SliderFieldPanel * component from Deitel and Deitel's Advanced * Java textbook. */ import javax.swing.*; import java.awt.*; import java.beans.*; public class TestSliderField extends JFrame implements PropertyChangeListener { // We use three sliders to set the color SliderFieldPanel redAmount; SliderFieldPanel greenAmount; SliderFieldPanel blueAmount; // The panel gives us a good background to set JPanel backPanel; // the constructor adds everything to the application public TestSliderField() { // instantiate the variables backPanel = new JPanel(); redAmount = new SliderFieldPanel(); greenAmount = new SliderFieldPanel(); blueAmount = new SliderFieldPanel(); // r, g, and b are between 0 and 255 redAmount.setMaximumValue(255); greenAmount.setMaximumValue(255); blueAmount.setMaximumValue(255); // We use the background color to know which slider is which redAmount.setBackground(Color.red); greenAmount.setBackground(Color.green); blueAmount.setBackground(Color.blue); // In general, it is better to have different handlers // for each event. In this case, however, the // code to be executed is identical. redAmount.addPropertyChangeListener(this); greenAmount.addPropertyChangeListener(this); blueAmount.addPropertyChangeListener(this); // We add the panel to the contentPane and then use // the panel for the sliders getContentPane().add(backPanel); backPanel.setLayout(null); // Set the components' properties redAmount.setSize(300, 30); redAmount.setLocation(10, 10); backPanel.add(redAmount); // Set the components' properties greenAmount.setSize(300, 30); greenAmount.setLocation(10, 50); backPanel.add(greenAmount); // Set the components' properties blueAmount.setSize(300, 30); blueAmount.setLocation(10, 90); backPanel.add(blueAmount); // set the initial color of the panel backPanel.setBackground(new Color( redAmount.getCurrentValue(), greenAmount.getCurrentValue(), blueAmount.getCurrentValue() )); // Set the basic JFrame Properties setSize(350, 200); setDefaultCloseOperation(EXIT_ON_CLOSE); show(); } // Whenever a slider changes value, this is called. public void propertyChange (PropertyChangeEvent e) { backPanel.setBackground(new Color( redAmount.getCurrentValue(), greenAmount.getCurrentValue(), blueAmount.getCurrentValue() )); } // A very simple main public static void main (String [] args) { new TestSliderField(); } }