~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/internal/contrib/randelshofer/quaqua/colorchooser/Crayons.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * @(#)Crayons.java  1.2  2006-04-23
 
3
 *
 
4
 * Copyright (c) 2005-2006 Werner Randelshofer
 
5
 * Staldenmattweg 2, Immensee, CH-6405, Switzerland.
 
6
 * All rights reserved.
 
7
 *
 
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.
 
13
 */
 
14
 
 
15
package org.pushingpixels.substance.internal.contrib.randelshofer.quaqua.colorchooser;
 
16
 
 
17
 
 
18
import java.awt.*;
 
19
import java.awt.event.*;
 
20
import java.awt.image.*;
 
21
import java.awt.geom.*;
 
22
import javax.swing.*;
 
23
import javax.swing.event.*;
 
24
 
 
25
import org.pushingpixels.substance.internal.contrib.randelshofer.quaqua.*;
 
26
import org.pushingpixels.substance.internal.contrib.randelshofer.quaqua.util.*;
 
27
 
 
28
/**
 
29
 * A panel which displays a selection of color crayons. The user can click at
 
30
 * a crayon to pick a color.
 
31
 *
 
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
 
36
 * and feels.
 
37
 * <br>1.0 August 28, 2005 Created.
 
38
 */
 
39
public class Crayons extends javax.swing.JPanel {
 
40
    
 
41
    /**
 
42
     * Shared crayons image.
 
43
     */
 
44
    private Image crayonsImage;
 
45
    
 
46
    /**
 
47
     * Coordinates of crayon shaped polygon.
 
48
     */
 
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
 
51
    
 
52
    /**
 
53
     * Current color.
 
54
     */
 
55
    private Color color = Color.white;
 
56
    /**
 
57
     * Selected crayon.
 
58
     */
 
59
    private Crayon selectedCrayon = null;
 
60
    
 
61
    /**
 
62
     * Crayon.
 
63
     */
 
64
    private class Crayon {
 
65
        Polygon shape;
 
66
        Color color;
 
67
        String name;
 
68
        
 
69
        public Crayon(Color color, String name, Polygon shape) {
 
70
            this.color = color;
 
71
            this.name = name;
 
72
            this.shape = shape;
 
73
        }
 
74
    }
 
75
    
 
76
    private class MouseHandler extends MouseAdapter {
 
77
        @Override
 
78
        public void mousePressed(MouseEvent evt) {
 
79
            int x = evt.getX();
 
80
            int y = evt.getY();
 
81
            if (x > 0 && x < crayonsImage.getWidth(Crayons.this)
 
82
            && y > 0 && y < crayonsImage.getHeight(Crayons.this)
 
83
            ) {
 
84
                for (int i=crayons.length - 1; i >= 0; i--) {
 
85
                    if (crayons[i].shape.contains(x, y)) {
 
86
                        setColor(crayons[i].color);
 
87
                        break;
 
88
                    }
 
89
                }
 
90
            }
 
91
        }
 
92
    }
 
93
    
 
94
    private MouseHandler mouseHandler;
 
95
    
 
96
    /**
 
97
     * Crayons.
 
98
     */
 
99
    private Crayon[] crayons;
 
100
    
 
101
    /**
 
102
     * Creates a new instance.
 
103
     */
 
104
    public Crayons() {
 
105
        initComponents();
 
106
        
 
107
        setForeground(new Color(0x808080));
 
108
        setPreferredSize(new Dimension(195, 208));
 
109
        setFont(UIManager.getFont("ColorChooser.crayonsFont"));
 
110
        
 
111
        crayonsImage = createCrayonsImage();
 
112
        crayons = createCrayons();
 
113
        
 
114
        mouseHandler = new MouseHandler();
 
115
        addMouseListener(mouseHandler);
 
116
    }
 
117
 
 
118
    protected Image createCrayonsImage() {
 
119
         return (Image) UIManager.get("ColorChooser.crayonsImage");
 
120
    }
 
121
    
 
122
    /**
 
123
     * Creates the crayons.
 
124
     * @return Array of crayons in z-order from bottom to top.
 
125
     */
 
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(
 
131
            colors[i],
 
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,
 
136
            (i / 8) * 20 + 23
 
137
            );
 
138
        }
 
139
        
 
140
        return crayons;
 
141
    }
 
142
    
 
143
    /**
 
144
     * Sets the current color.
 
145
     * This results in a selection of a crayon, if a crayon with the same
 
146
     * RGB values exists.
 
147
     */
 
148
    public void setColor(Color newValue) {
 
149
        Color oldValue = color;
 
150
        color = newValue;
 
151
        
 
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];
 
157
            }
 
158
        }
 
159
        if (newSelectedCrayon != selectedCrayon) {
 
160
            selectedCrayon = newSelectedCrayon;
 
161
            repaint();
 
162
        }
 
163
        
 
164
        firePropertyChange("Color", oldValue, newValue);
 
165
    }
 
166
    
 
167
    /**
 
168
     * Returns the current color.
 
169
     */
 
170
    public Color getColor() {
 
171
        return color;
 
172
    }
 
173
    
 
174
    @Override
 
175
    public void paintComponent(Graphics gr) {
 
176
        Graphics2D g = (Graphics2D) gr;
 
177
        Object oldHints = QuaquaUtilities.beginGraphics((Graphics2D) g);
 
178
        
 
179
        g.drawImage(crayonsImage, 0, 0, this);
 
180
        
 
181
        
 
182
        if (selectedCrayon != null) {
 
183
            /*
 
184
            g.setColor(new Color(0x60ffffff & selectedCrayon.color.getRGB(),true));
 
185
            g.fill(selectedCrayon.shape);
 
186
             */
 
187
            g.setColor(getForeground());
 
188
            FontMetrics fm = g.getFontMetrics();
 
189
            int nameWidth = fm.stringWidth(selectedCrayon.name);
 
190
            g.drawString(
 
191
            selectedCrayon.name,
 
192
            (crayonsImage.getWidth(this) - nameWidth) / 2,
 
193
            fm.getAscent() + 1
 
194
            );
 
195
        }
 
196
        QuaquaUtilities.endGraphics((Graphics2D) g, oldHints);
 
197
    }
 
198
    
 
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.
 
203
     */
 
204
    private void initComponents() {//GEN-BEGIN:initComponents
 
205
        
 
206
        setLayout(new java.awt.BorderLayout());
 
207
        
 
208
    }//GEN-END:initComponents
 
209
    
 
210
    
 
211
    // Variables declaration - do not modify//GEN-BEGIN:variables
 
212
    // End of variables declaration//GEN-END:variables
 
213
    
 
214
}