6 import java.util.Vector;
7 import java.awt.event.*;
11 public class ColorChooser implements TextListener, AdjustmentListener {
12 Vector colors, sliders;
21 public ColorChooser(String title, Color initialColor,
boolean useHSB) {
23 this.initialColor = initialColor;
24 red = initialColor.getRed();
25 green = initialColor.getGreen();
26 blue = initialColor.getBlue();
27 mono = red==green && green==blue;
34 gd.addSlider(
"Red:", 0, 255, red);
35 gd.addSlider(
"Green:", 0, 255, green);
36 gd.addSlider(
"Blue:", 0, 255, blue);
37 panel =
new ColorPanel(initialColor);
38 gd.
addPanel(panel, GridBagConstraints.CENTER,
new Insets(10, 0, 0, 0));
40 for (
int i=0; i<colors.size(); i++)
41 ((TextField)colors.elementAt(i)).addTextListener(
this);
43 for (
int i=0; i<sliders.size(); i++)
44 ((Scrollbar)sliders.elementAt(i)).addAdjustmentListener(
this);
46 if (gd.wasCanceled())
return null;
50 return new Color(red, green, blue);
53 public void textValueChanged(TextEvent e) {
54 int red = (int)
Tools.
parseDouble(((TextField)colors.elementAt(0)).getText());
55 int green = (int)
Tools.
parseDouble(((TextField)colors.elementAt(1)).getText());
56 int blue = (int)
Tools.
parseDouble(((TextField)colors.elementAt(2)).getText());
57 panel.setColor(
new Color(red, green, blue));
61 public synchronized void adjustmentValueChanged(AdjustmentEvent e) {
62 Object source = e.getSource();
63 for (
int i=0; i<sliders.size(); i++) {
64 if (source==sliders.elementAt(i)) {
65 Scrollbar sb = (Scrollbar)source;
66 TextField tf = (TextField)colors.elementAt(i);
68 String red = tf.getText();
69 TextField tf1 = (TextField)colors.elementAt(1);
70 TextField tf2 = (TextField)colors.elementAt(2);
74 if (i!=0) mono =
false;
81 class ColorPanel
extends Panel {
82 static final int WIDTH=100, HEIGHT=50;
89 public Dimension getPreferredSize() {
90 return new Dimension(WIDTH, HEIGHT);
93 void setColor(Color c) {
97 public Dimension getMinimumSize() {
98 return new Dimension(WIDTH, HEIGHT);
101 public void paint(Graphics g) {
103 g.fillRect(0, 0, WIDTH, HEIGHT);
104 g.setColor(Color.black);
105 g.drawRect(0, 0, WIDTH-1, HEIGHT-1);