|
|
Line 1: |
Line 1: |
| Back to [[Into Java - Part XVI]]
| | [[Category:For Removal]] |
| <PRE>
| |
| import javax.swing.*;
| |
| import javax.swing.event.*;
| |
| import java.awt.event.*;
| |
| import java.awt.*;
| |
| //import java.util.*;
| |
| | |
| public class SlidePanel extends JPanel {
| |
| | |
| private PaintBox owner; // the main JFrame
| |
| private JSlider red;
| |
| private JSlider green;
| |
| private JSlider blue;
| |
| | |
| public SlidePanel(PaintBox o) {
| |
| owner = o; // reference to the main frame
| |
| | |
| Box box = Box.createHorizontalBox(); // a holder
| |
| | |
| box.add(red = addSlider(Color.red, true));
| |
| box.add(green = addSlider(Color.green, true));
| |
| box.add(blue = addSlider(Color.blue, false));
| |
| add(box);
| |
| }
| |
| | |
| /*
| |
| * A helper medthod to avoid duplicated code
| |
| */
| |
| private JSlider addSlider(Color c, boolean bol) {
| |
| JSlider s = new JSlider(SwingConstants.VERTICAL, // direction
| |
| 0, // min value
| |
| 255, // max value
| |
| 0); // initial value
| |
| s.setMinorTickSpacing(25);
| |
| if (bol) {
| |
| s.setMajorTickSpacing(50);
| |
| }
| |
| s.setPaintTicks(true);
| |
| s.setForeground(c); // foreground color
| |
| | |
| s.addChangeListener(new SliderChangeListener());
| |
| | |
| return s;
| |
| }
| |
| | |
| public void setColor() {
| |
| int r = red.getValue();
| |
| int g = green.getValue();
| |
| int b = blue.getValue();
| |
| owner.setColor(r, g, b);
| |
| }
| |
| | |
| /*
| |
| * An anonymous listener class that have full access to
| |
| * the variables and methods of the "surrounding" object
| |
| */
| |
| class SliderChangeListener implements ChangeListener {
| |
| public void stateChanged(ChangeEvent e) {
| |
| setColor();
| |
| }
| |
| }
| |
| }
| |
| </PRE>
| |
| | |
| [[Category:Java Articles]] | |