1 /* 2 * 3 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * - Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * - Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * - Neither the name of Oracle nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 34 import javax.swing.*; 35 import javax.swing.event.*; 36 import javax.swing.text.*; 37 import javax.swing.border.*; 38 import javax.swing.colorchooser.*; 39 import javax.swing.filechooser.*; 40 import javax.accessibility.*; 41 42 import java.awt.*; 43 import java.awt.event.*; 44 import java.beans.*; 45 import java.util.*; 46 import java.io.*; 47 import java.applet.*; 48 import java.net.*; 49 50 /** 51 * JColorChooserDemo 52 * 53 * @author Jeff Dinkins 54 */ 55 public class ColorChooserDemo extends DemoModule { 56 57 BezierAnimationPanel bezAnim; 58 JButton outerColorButton = null; 59 JButton backgroundColorButton = null; 60 JButton gradientAButton = null; 61 JButton gradientBButton = null; 62 63 // to store the color chosen from the JColorChooser 64 private Color chosen; 65 66 /** 67 * main method allows us to run as a standalone demo. 68 */ 69 public static void main(String[] args) { 70 ColorChooserDemo demo = new ColorChooserDemo(null); 71 demo.mainImpl(); 72 } 73 74 75 /** 76 * ColorChooserDemo Constructor 77 */ 78 public ColorChooserDemo(SwingSet2 swingset) { 79 // Set the title for this demo, and an icon used to represent this 80 // demo inside the SwingSet2 app. 81 super(swingset, "ColorChooserDemo", "toolbar/JColorChooser.gif"); 82 83 // Create the bezier animation panel to put in the center of the panel. 84 bezAnim = new BezierAnimationPanel(); 85 86 outerColorButton = new JButton(getString("ColorChooserDemo.outer_line")); 87 outerColorButton.setIcon(new ColorSwatch("OuterLine", bezAnim)); 88 89 backgroundColorButton = new JButton(getString("ColorChooserDemo.background")); 90 backgroundColorButton.setIcon(new ColorSwatch("Background", bezAnim)); 91 92 gradientAButton = new JButton(getString("ColorChooserDemo.grad_a")); 93 gradientAButton.setIcon(new ColorSwatch("GradientA", bezAnim)); 94 95 gradientBButton = new JButton(getString("ColorChooserDemo.grad_b")); 96 gradientBButton.setIcon(new ColorSwatch("GradientB", bezAnim)); 97 98 ActionListener l = new ActionListener() { 99 public void actionPerformed(ActionEvent e) { 100 Color current = bezAnim.getOuterColor(); 101 102 if(e.getSource() == backgroundColorButton) { 103 current = bezAnim.getBackgroundColor(); 104 } else if(e.getSource() == gradientAButton) { 105 current = bezAnim.getGradientColorA(); 106 } else if(e.getSource() == gradientBButton) { 107 current = bezAnim.getGradientColorB(); 108 } 109 110 final JColorChooser chooser = new JColorChooser(current != null ? 111 current : 112 Color.WHITE); 113 if (getSwingSet2() != null && getSwingSet2().isDragEnabled()) { 114 chooser.setDragEnabled(true); 115 } 116 117 chosen = null; 118 ActionListener okListener = new ActionListener() { 119 public void actionPerformed(ActionEvent ae) { 120 chosen = chooser.getColor(); 121 } 122 }; 123 124 JDialog dialog = JColorChooser.createDialog(getDemoPanel(), 125 getString("ColorChooserDemo.chooser_title"), 126 true, 127 chooser, 128 okListener, 129 null); 130 131 dialog.setVisible(true); 132 133 if(e.getSource() == outerColorButton) { 134 bezAnim.setOuterColor(chosen); 135 } else if(e.getSource() == backgroundColorButton) { 136 bezAnim.setBackgroundColor(chosen); 137 } else if(e.getSource() == gradientAButton) { 138 bezAnim.setGradientColorA(chosen); 139 } else { 140 bezAnim.setGradientColorB(chosen); 141 } 142 } 143 }; 144 145 outerColorButton.addActionListener(l); 146 backgroundColorButton.addActionListener(l); 147 gradientAButton.addActionListener(l); 148 gradientBButton.addActionListener(l); 149 150 // Add everything to the panel 151 JPanel p = getDemoPanel(); 152 p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); 153 154 // Add control buttons 155 JPanel buttonPanel = new JPanel(); 156 buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); 157 158 buttonPanel.add(backgroundColorButton); 159 buttonPanel.add(Box.createRigidArea(new Dimension(15, 1))); 160 161 buttonPanel.add(gradientAButton); 162 buttonPanel.add(Box.createRigidArea(new Dimension(15, 1))); 163 164 buttonPanel.add(gradientBButton); 165 buttonPanel.add(Box.createRigidArea(new Dimension(15, 1))); 166 167 buttonPanel.add(outerColorButton); 168 169 // Add the panel midway down the panel 170 p.add(Box.createRigidArea(new Dimension(1, 10))); 171 p.add(buttonPanel); 172 p.add(Box.createRigidArea(new Dimension(1, 5))); 173 p.add(bezAnim); 174 } 175 176 class ColorSwatch implements Icon { 177 String gradient; 178 BezierAnimationPanel bez; 179 180 public ColorSwatch(String g, BezierAnimationPanel b) { 181 bez = b; 182 gradient = g; 183 } 184 185 public int getIconWidth() { 186 return 11; 187 } 188 189 public int getIconHeight() { 190 return 11; 191 } 192 193 public void paintIcon(Component c, Graphics g, int x, int y) { 194 g.setColor(Color.black); 195 g.fillRect(x, y, getIconWidth(), getIconHeight()); 196 if(gradient.equals("GradientA")) { 197 g.setColor(bez.getGradientColorA()); 198 } else if(gradient.equals("GradientB")) { 199 g.setColor(bez.getGradientColorB()); 200 } else if(gradient.equals("Background")) { 201 g.setColor(bez.getBackgroundColor()); 202 } else if(gradient.equals("OuterLine")) { 203 g.setColor(bez.getOuterColor()); 204 } 205 g.fillRect(x+2, y+2, getIconWidth()-4, getIconHeight()-4); 206 } 207 } 208 209 }