/** * Class which implements an applet which displays the color selected * using three scrollbars. It illustrates the use of "listeners" * Written by Kim Bruce, 10/3/99 */ import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class ColorMixer implements ActionListener { // 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); // The panel where the mixed color is displayed. private ColorPanel colorPanel; // Set up Scrollbars and JLabels on panels public ColorMixer() { // Create the window JFrame frame = new JFrame(); frame.setSize (new Dimension (300, 200)); // 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 = frame.getContentPane(); contentPane.add(selectorPanel, BorderLayout.SOUTH); colorPanel = new ColorPanel(); contentPane.add(colorPanel, BorderLayout.CENTER); frame.setVisible(true); } public void actionPerformed(ActionEvent evt) { try { // get component color values int redValue = Integer.parseInt(redValueField.getText()); int greenValue = Integer.parseInt(greenValueField.getText()); int blueValue = Integer.parseInt(blueValueField.getText()); // Create the new color and make it the color for colorRect colorPanel.setColor (new Color(redValue, greenValue, blueValue)); colorPanel.repaint(); } catch (NumberFormatException e) { JOptionPane.showMessageDialog( colorPanel, "Enter only numbers: " + e.getMessage()); } catch (IllegalArgumentException e) { JOptionPane.showMessageDialog( colorPanel, "Enter only 0..255: " + e.getMessage()); } } public static void main (String[] args) { new ColorMixer(); } }