|
1 /* |
|
2 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 package com.sun.swingset3.demos.spinner; |
|
24 |
|
25 import javax.swing.*; |
|
26 import java.awt.*; |
|
27 import javax.swing.event.ChangeListener; |
|
28 import javax.swing.event.ChangeEvent; |
|
29 |
|
30 import com.sun.swingset3.demos.ResourceManager; |
|
31 |
|
32 /** |
|
33 * @author Mikhail Lapshin |
|
34 */ |
|
35 public class PaletteChooser extends JPanel { |
|
36 |
|
37 private static final int MIN_COLOR = 50; |
|
38 private static final int MAX_COLOR = 255; |
|
39 private static final int R_STEPS = 5; |
|
40 private static final int G_STEPS = 5; |
|
41 private static final int B_STEPS = 5; |
|
42 private static final int R_ANGLE = 270; |
|
43 private static final int G_ANGLE = 90; |
|
44 private static final int B_ANGLE = 0; |
|
45 |
|
46 public static final String PALETTE_PROPERTY_NAME = "palette"; |
|
47 |
|
48 private final ResourceManager resourceManager; |
|
49 private Palette palette; |
|
50 private final JPaletteShower shower; |
|
51 private final ChangeListener changeListener; |
|
52 |
|
53 private JSpinner rsSpinner; |
|
54 private JSpinner gsSpinner; |
|
55 private JSpinner bsSpinner; |
|
56 private JSpinner raSpinner; |
|
57 private JSpinner gaSpinner; |
|
58 private JSpinner baSpinner; |
|
59 |
|
60 public PaletteChooser(ResourceManager resourceManager) { |
|
61 this.resourceManager = resourceManager; |
|
62 palette = new Palette(MAX_COLOR - MIN_COLOR, MIN_COLOR, MAX_COLOR, |
|
63 Math.toRadians(R_ANGLE), Math.toRadians(G_ANGLE), |
|
64 Math.toRadians(B_ANGLE), R_STEPS, G_STEPS, B_STEPS); |
|
65 shower = new JPaletteShower(palette, 250, 25); |
|
66 |
|
67 //<snip>Use single change listener for several spinners |
|
68 changeListener = new ChangeListener() { |
|
69 @Override |
|
70 public void stateChanged(ChangeEvent e) { |
|
71 setPalette(createPalette()); |
|
72 shower.setPalette(palette); |
|
73 repaint(); |
|
74 } |
|
75 }; |
|
76 //</snip> |
|
77 |
|
78 setBorder(BorderFactory.createTitledBorder( |
|
79 resourceManager.getString("SpinnerDemo.colorPalette"))); |
|
80 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); |
|
81 add(shower); |
|
82 add(createControlPanel()); |
|
83 } |
|
84 |
|
85 private double toRadians(JSpinner spinner) { |
|
86 return Math.toRadians(getIntValue(spinner)); |
|
87 } |
|
88 |
|
89 private Palette createPalette() { |
|
90 return new Palette(getWidth(), MIN_COLOR, MAX_COLOR, |
|
91 toRadians(raSpinner), toRadians(gaSpinner), |
|
92 toRadians(baSpinner), getIntValue(rsSpinner), |
|
93 getIntValue(gsSpinner), getIntValue(bsSpinner)); |
|
94 } |
|
95 |
|
96 private static int getIntValue(JSpinner spinner) { |
|
97 return (Integer) spinner.getValue(); |
|
98 } |
|
99 |
|
100 private JPanel createControlPanel() { |
|
101 JPanel controlPanel = new JPanel(); |
|
102 controlPanel.setLayout(new GridLayout(1, 2)); |
|
103 controlPanel.add(createStepPanel()); |
|
104 controlPanel.add(createStartAnglePanel()); |
|
105 return controlPanel; |
|
106 } |
|
107 |
|
108 private JPanel createStartAnglePanel() { |
|
109 JSpinnerPanel startAnglePanel = new JSpinnerPanel(); |
|
110 startAnglePanel.setBorder(BorderFactory.createTitledBorder( |
|
111 resourceManager.getString("SpinnerDemo.startAngles"))); |
|
112 |
|
113 raSpinner = createAngleSpinner(R_ANGLE, "SpinnerDemo.r", startAnglePanel); |
|
114 gaSpinner = createAngleSpinner(G_ANGLE, "SpinnerDemo.g", startAnglePanel); |
|
115 baSpinner = createAngleSpinner(B_ANGLE, "SpinnerDemo.b", startAnglePanel); |
|
116 |
|
117 return startAnglePanel; |
|
118 } |
|
119 |
|
120 private JPanel createStepPanel() { |
|
121 JSpinnerPanel stepPanel = new JSpinnerPanel(); |
|
122 stepPanel.setBorder(BorderFactory.createTitledBorder( |
|
123 resourceManager.getString("SpinnerDemo.steps"))); |
|
124 |
|
125 rsSpinner = createStepSpinner(R_STEPS, "SpinnerDemo.r", stepPanel); |
|
126 gsSpinner = createStepSpinner(G_STEPS, "SpinnerDemo.g", stepPanel); |
|
127 bsSpinner = createStepSpinner(B_STEPS, "SpinnerDemo.b", stepPanel); |
|
128 |
|
129 return stepPanel; |
|
130 } |
|
131 |
|
132 private JSpinner createAngleSpinner(int startAngle, String resourceName, |
|
133 JSpinnerPanel parent) { |
|
134 SpinnerModel model = new SpinnerNumberModel(startAngle, 0, 360, 10); |
|
135 return createSpinner(model, resourceName, parent); |
|
136 } |
|
137 |
|
138 private JSpinner createStepSpinner(int startSteps, String resourceName, |
|
139 JSpinnerPanel parent) { |
|
140 SpinnerModel model = new SpinnerNumberModel(startSteps, 1, 1000, 1); |
|
141 return createSpinner(model, resourceName, parent); |
|
142 } |
|
143 |
|
144 private JSpinner createSpinner(SpinnerModel model, String resourceName, |
|
145 JSpinnerPanel parent) { |
|
146 |
|
147 //<snip>Create spinner |
|
148 JSpinner spinner = new JSpinner(model); |
|
149 //</snip> |
|
150 //<snip>Use single change listener for several spinners |
|
151 spinner.addChangeListener(changeListener); |
|
152 //</snip> |
|
153 parent.addSpinner(resourceManager.getString(resourceName), spinner); |
|
154 return spinner; |
|
155 } |
|
156 |
|
157 public Palette getPalette() { |
|
158 return palette; |
|
159 } |
|
160 |
|
161 private void setPalette(Palette palette) { |
|
162 Palette oldValue = this.palette; |
|
163 this.palette = palette; |
|
164 firePropertyChange(PALETTE_PROPERTY_NAME, oldValue, palette); |
|
165 } |
|
166 } |