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