SlidePanel.java: Difference between revisions
Appearance
Created page with " Back to Into Java - Part XVI <PRE> import javax.swing.*; import javax.swing.event.*; import java.awt.event.*; import java.awt.*; //import java.util.*; public class Slid..." |
mNo edit summary |
||
| Line 1: | Line 1: | ||
Back to [[Into Java - Part XVI]] | Back to [[Into Java - Part XVI]] | ||
<PRE> | <PRE> | ||
import javax.swing.*; | import javax.swing.*; | ||
| Line 67: | Line 62: | ||
} | } | ||
} | } | ||
</PRE> | </PRE> | ||
Revision as of 22:18, 13 December 2017
Back to Into Java - Part XVI
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();
}
}
}