1
36
37
40
41
42 import javax.swing.*;
43 import javax.swing.event.*;
44 import javax.swing.text.*;
45 import javax.swing.border.*;
46 import javax.swing.colorchooser.*;
47 import javax.swing.filechooser.*;
48 import javax.accessibility.*;
49
50 import java.awt.*;
51 import java.awt.event.*;
52 import java.beans.*;
53 import java.util.*;
54 import java.io.*;
55 import java.applet.*;
56 import java.net.*;
57
58
64 public class ColorChooserDemo extends DemoModule {
65
66 BezierAnimationPanel bezAnim;
67 JButton outerColorButton = null;
68 JButton backgroundColorButton = null;
69 JButton gradientAButton = null;
70 JButton gradientBButton = null;
71
72 private Color chosen;
74
75
78 public static void main(String[] args) {
79 ColorChooserDemo demo = new ColorChooserDemo(null);
80 demo.mainImpl();
81 }
82
83
84
87 public ColorChooserDemo(SwingSet2 swingset) {
88 super(swingset, "ColorChooserDemo", "toolbar/JColorChooser.gif");
91
92 bezAnim = new BezierAnimationPanel();
94
95 outerColorButton = new JButton(getString("ColorChooserDemo.outer_line"));
96 outerColorButton.setIcon(new ColorSwatch("OuterLine", bezAnim));
97
98 backgroundColorButton = new JButton(getString("ColorChooserDemo.background"));
99 backgroundColorButton.setIcon(new ColorSwatch("Background", bezAnim));
100
101 gradientAButton = new JButton(getString("ColorChooserDemo.grad_a"));
102 gradientAButton.setIcon(new ColorSwatch("GradientA", bezAnim));
103
104 gradientBButton = new JButton(getString("ColorChooserDemo.grad_b"));
105 gradientBButton.setIcon(new ColorSwatch("GradientB", bezAnim));
106
107 ActionListener l = new ActionListener() {
108 public void actionPerformed(ActionEvent e) {
109 Color current = bezAnim.getOuterColor();
110
111 if(e.getSource() == backgroundColorButton) {
112 current = bezAnim.getBackgroundColor();
113 } else if(e.getSource() == gradientAButton) {
114 current = bezAnim.getGradientColorA();
115 } else if(e.getSource() == gradientBButton) {
116 current = bezAnim.getGradientColorB();
117 }
118
119 final JColorChooser chooser = new JColorChooser(current != null ?
120 current :
121 Color.WHITE);
122 if (getSwingSet2() != null && getSwingSet2().isDragEnabled()) {
123 chooser.setDragEnabled(true);
124 }
125
126 chosen = null;
127 ActionListener okListener = new ActionListener() {
128 public void actionPerformed(ActionEvent ae) {
129 chosen = chooser.getColor();
130 }
131 };
132
133 JDialog dialog = JColorChooser.createDialog(getDemoPanel(),
134 getString("ColorChooserDemo.chooser_title"),
135 true,
136 chooser,
137 okListener,
138 null);
139
140 dialog.show();
141
142 if(e.getSource() == outerColorButton) {
143 bezAnim.setOuterColor(chosen);
144 } else if(e.getSource() == backgroundColorButton) {
145 bezAnim.setBackgroundColor(chosen);
146 } else if(e.getSource() == gradientAButton) {
147 bezAnim.setGradientColorA(chosen);
148 } else {
149 bezAnim.setGradientColorB(chosen);
150 }
151 }
152 };
153
154 outerColorButton.addActionListener(l);
155 backgroundColorButton.addActionListener(l);
156 gradientAButton.addActionListener(l);
157 gradientBButton.addActionListener(l);
158
159 JPanel p = getDemoPanel();
161 p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
162
163 JPanel buttonPanel = new JPanel();
165 buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
166
167 buttonPanel.add(backgroundColorButton);
168 buttonPanel.add(Box.createRigidArea(new Dimension(15, 1)));
169
170 buttonPanel.add(gradientAButton);
171 buttonPanel.add(Box.createRigidArea(new Dimension(15, 1)));
172
173 buttonPanel.add(gradientBButton);
174 buttonPanel.add(Box.createRigidArea(new Dimension(15, 1)));
175
176 buttonPanel.add(outerColorButton);
177
178 p.add(Box.createRigidArea(new Dimension(1, 10)));
180 p.add(buttonPanel);
181 p.add(Box.createRigidArea(new Dimension(1, 5)));
182 p.add(bezAnim);
183 }
184
185 class ColorSwatch implements Icon {
186 String gradient;
187 BezierAnimationPanel bez;
188
189 public ColorSwatch(String g, BezierAnimationPanel b) {
190 bez = b;
191 gradient = g;
192 }
193
194 public int getIconWidth() {
195 return 11;
196 }
197
198 public int getIconHeight() {
199 return 11;
200 }
201
202 public void paintIcon(Component c, Graphics g, int x, int y) {
203 g.setColor(Color.black);
204 g.fillRect(x, y, getIconWidth(), getIconHeight());
205 if(gradient.equals("GradientA")) {
206 g.setColor(bez.getGradientColorA());
207 } else if(gradient.equals("GradientB")) {
208 g.setColor(bez.getGradientColorB());
209 } else if(gradient.equals("Background")) {
210 g.setColor(bez.getBackgroundColor());
211 } else if(gradient.equals("OuterLine")) {
212 g.setColor(bez.getOuterColor());
213 }
214 g.fillRect(x+2, y+2, getIconWidth()-4, getIconHeight()-4);
215 }
216 }
217
218 }
219