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