ColorChooserDemo.java  
1   /*
2    * @(#)ColorChooserDemo.java    1.11 04/07/26
3    * 
4    * Copyright (c) 2004 Sun Microsystems, Inc. All Rights Reserved.
5    * 
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are met:
8    * 
9    * -Redistribution of source code must retain the above copyright notice, this
10   *  list of conditions and the following disclaimer.
11   * 
12   * -Redistribution in binary form must reproduce the above copyright notice, 
13   *  this list of conditions and the following disclaimer in the documentation
14   *  and/or other materials provided with the distribution.
15   * 
16   * Neither the name of Sun Microsystems, Inc. or the names of contributors may 
17   * be used to endorse or promote products derived from this software without 
18   * specific prior written permission.
19   * 
20   * This software is provided "AS IS," without a warranty of any kind. ALL 
21   * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22   * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23   * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24   * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25   * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26   * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST 
27   * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, 
28   * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY 
29   * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, 
30   * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31   * 
32   * You acknowledge that this software is not designed, licensed or intended
33   * for use in the design, construction, operation or maintenance of any
34   * nuclear facility.
35   */
36  
37  /*
38   * @(#)ColorChooserDemo.java    1.11 04/07/26
39   */
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  /**
59   * JColorChooserDemo
60   *
61   * @version 1.1 07/16/99
62   * @author Jeff Dinkins
63   */
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      // to store the color chosen from the JColorChooser
73      private Color chosen;
74  
75      /**
76       * main method allows us to run as a standalone demo.
77       */
78      public static void main(String[] args) {
79      ColorChooserDemo demo = new ColorChooserDemo(null);
80      demo.mainImpl();
81      }
82  
83  
84      /**
85       * ColorChooserDemo Constructor
86       */
87      public ColorChooserDemo(SwingSet2 swingset) {
88      // Set the title for this demo, and an icon used to represent this
89      // demo inside the SwingSet2 app.
90      super(swingset, "ColorChooserDemo", "toolbar/JColorChooser.gif");
91  
92      // Create the bezier animation panel to put in the center of the panel.
93      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     // Add everything to the panel
160     JPanel p = getDemoPanel();
161     p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
162 
163     // Add control buttons
164     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     // Add the panel midway down the panel
179     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