2
* @(#)Crayons.java 1.2 2006-04-23
4
* Copyright (c) 2005-2006 Werner Randelshofer
5
* Staldenmattweg 2, Immensee, CH-6405, Switzerland.
8
* This software is the confidential and proprietary information of
9
* Werner Randelshofer. ("Confidential Information"). You shall not
10
* disclose such Confidential Information and shall use it only in
11
* accordance with the terms of the license agreement you entered into
12
* with Werner Randelshofer.
15
package org.pushingpixels.substance.internal.contrib.randelshofer.quaqua.colorchooser;
19
import java.awt.event.*;
20
import java.awt.image.*;
21
import java.awt.geom.*;
23
import javax.swing.event.*;
25
import org.pushingpixels.substance.internal.contrib.randelshofer.quaqua.*;
26
import org.pushingpixels.substance.internal.contrib.randelshofer.quaqua.util.*;
29
* A panel which displays a selection of color crayons. The user can click at
30
* a crayon to pick a color.
32
* @author Werner Randelshofer
33
* @version 1.2 2006-04-23 Retrieve labels directly from UIManager.
34
* <br>1.1.1 2005-11-07 Get "labels" resource bundle from UIManager.
35
* <br>1.1 2005-08-30 Rearranged code to ease the creation of derived look
37
* <br>1.0 August 28, 2005 Created.
39
public class Crayons extends javax.swing.JPanel {
42
* Shared crayons image.
44
private Image crayonsImage;
47
* Coordinates of crayon shaped polygon.
49
private final static int[] crayonXPoints = { 10, 12, 20, 22, 22, 0, 0, 2 }; // xpoints
50
private final static int[] crayonYPoints = { 0, 0, 21, 21, 104, 104, 21, 21 }; // ypoints
55
private Color color = Color.white;
59
private Crayon selectedCrayon = null;
64
private class Crayon {
69
public Crayon(Color color, String name, Polygon shape) {
76
private class MouseHandler extends MouseAdapter {
78
public void mousePressed(MouseEvent evt) {
81
if (x > 0 && x < crayonsImage.getWidth(Crayons.this)
82
&& y > 0 && y < crayonsImage.getHeight(Crayons.this)
84
for (int i=crayons.length - 1; i >= 0; i--) {
85
if (crayons[i].shape.contains(x, y)) {
86
setColor(crayons[i].color);
94
private MouseHandler mouseHandler;
99
private Crayon[] crayons;
102
* Creates a new instance.
107
setForeground(new Color(0x808080));
108
setPreferredSize(new Dimension(195, 208));
109
setFont(UIManager.getFont("ColorChooser.crayonsFont"));
111
crayonsImage = createCrayonsImage();
112
crayons = createCrayons();
114
mouseHandler = new MouseHandler();
115
addMouseListener(mouseHandler);
118
protected Image createCrayonsImage() {
119
return (Image) UIManager.get("ColorChooser.crayonsImage");
123
* Creates the crayons.
124
* @return Array of crayons in z-order from bottom to top.
126
protected Crayon[] createCrayons() {
127
Color[] colors = DefaultPalettes.CRAYONS;
128
crayons = new Crayon[colors.length];
129
for (int i=0; i < colors.length; i++) {
130
crayons[i] = new Crayon(
132
UIManager.getString("ColorChooser.crayon."+Integer.toHexString(0xff000000|colors[i].getRGB()).substring(2)),
133
new Polygon((int[]) crayonXPoints.clone(), (int[]) crayonYPoints.clone(), crayonXPoints.length));
134
crayons[i].shape.translate(
135
(i % 8) * 22 + 4 +((i / 8) % 2) * 11,
144
* Sets the current color.
145
* This results in a selection of a crayon, if a crayon with the same
148
public void setColor(Color newValue) {
149
Color oldValue = color;
152
Crayon newSelectedCrayon = null;
153
int newRGB = newValue.getRGB() & 0xffffff;
154
for (int i=0; i < crayons.length; i++) {
155
if ((crayons[i].color.getRGB() & 0xffffff) == newRGB) {
156
newSelectedCrayon = crayons[i];
159
if (newSelectedCrayon != selectedCrayon) {
160
selectedCrayon = newSelectedCrayon;
164
firePropertyChange("Color", oldValue, newValue);
168
* Returns the current color.
170
public Color getColor() {
175
public void paintComponent(Graphics gr) {
176
Graphics2D g = (Graphics2D) gr;
177
Object oldHints = QuaquaUtilities.beginGraphics((Graphics2D) g);
179
g.drawImage(crayonsImage, 0, 0, this);
182
if (selectedCrayon != null) {
184
g.setColor(new Color(0x60ffffff & selectedCrayon.color.getRGB(),true));
185
g.fill(selectedCrayon.shape);
187
g.setColor(getForeground());
188
FontMetrics fm = g.getFontMetrics();
189
int nameWidth = fm.stringWidth(selectedCrayon.name);
192
(crayonsImage.getWidth(this) - nameWidth) / 2,
196
QuaquaUtilities.endGraphics((Graphics2D) g, oldHints);
199
/** This method is called from within the constructor to
200
* initialize the form.
201
* WARNING: Do NOT modify this code. The content of this method is
202
* always regenerated by the Form Editor.
204
private void initComponents() {//GEN-BEGIN:initComponents
206
setLayout(new java.awt.BorderLayout());
208
}//GEN-END:initComponents
211
// Variables declaration - do not modify//GEN-BEGIN:variables
212
// End of variables declaration//GEN-END:variables