/Users/joshy/projects/current/swingx/src/java/org/jdesktop/swingx/color/EyeDropperColorChooserPanel.java |
1
2
3
4
5
6
7 package org.jdesktop.swingx.color;
8
9 import java.awt.*;
10 import java.awt.event.MouseEvent;
11 import java.awt.geom.Point2D;
12 import java.awt.image.BufferedImage;
13 import java.beans.PropertyChangeEvent;
14 import java.beans.PropertyChangeListener;
15 import javax.swing.*;
16 import javax.swing.colorchooser.AbstractColorChooserPanel;
17 import javax.swing.event.MouseInputAdapter;
18 import org.jdesktop.swingx.JXColorSelectionButton;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class EyeDropperColorChooserPanel extends AbstractColorChooserPanel {
44 private Color oldColor;
45
46
47
48
49 public static void main(String ... args) {
50 SwingUtilities.invokeLater(new Runnable() {
51 public void run() {
52 JColorChooser chooser = new JColorChooser();
53 chooser.addChooserPanel(new EyeDropperColorChooserPanel());
54 JFrame frame = new JFrame();
55 frame.add(chooser);
56 frame.pack();
57 frame.setVisible(true);
58 }
59 });
60 }
61
62
63
64
65 public EyeDropperColorChooserPanel() {
66 initComponents();
67 MouseInputAdapter mia = new MouseInputAdapter() {
68 public void mousePressed(MouseEvent evt) {
69 }
70 public void mouseDragged(MouseEvent evt) {
71 Point pt = evt.getPoint();
72 SwingUtilities.convertPointToScreen(pt,evt.getComponent());
73 ((MagnifyingPanel)magPanel).setMagPoint(pt);
74 }
75 public void mouseReleased(MouseEvent evt) {
76 Color newColor = new Color(((MagnifyingPanel)magPanel).activeColor);
77 oldColor = newColor;
78 getColorSelectionModel().setSelectedColor(oldColor);
79 }
80 };
81 eyeDropper.addMouseListener(mia);
82 eyeDropper.addMouseMotionListener(mia);
83 try {
84 eyeDropper.setIcon(new ImageIcon(getClass().getResource("mag.png")));
85 eyeDropper.setText("");
86 } catch (Exception ex) {
87
88 }
89
90 magPanel.addPropertyChangeListener(new PropertyChangeListener() {
91 public void propertyChange(PropertyChangeEvent evt) {
92 Color color = new Color(((MagnifyingPanel)magPanel).activeColor);
93 activeColor.setBackground(color);
94 hexColor.setText(ColorUtil.toHexString(color).substring(1));
95 rgbColor.setText(color.getRed() +"," + color.getGreen() + "," + color.getBlue());
96 }
97 });
98 }
99
100 private class MagnifyingPanel extends JPanel {
101 private Point2D point;
102 private int activeColor;
103 public void setMagPoint(Point2D point) {
104 this.point = point;
105 repaint();
106 }
107 public void paintComponent(Graphics g) {
108 if(point != null) {
109 Rectangle rect = new Rectangle((int)point.getX()-10,(int)point.getY()-10,20,20);
110 try {
111 BufferedImage img =new Robot().createScreenCapture(rect);
112 g.drawImage(img,0,0,getWidth(),getHeight(),null);
113 int oldColor = activeColor;
114 activeColor = img.getRGB(img.getWidth()/2,img.getHeight()/2);
115 firePropertyChange("activeColor", oldColor, activeColor);
116 } catch (AWTException ex) {
117 ex.printStackTrace();
118 }
119 }
120 g.setColor(Color.black);
121 g.drawRect(getWidth()/2 - 5, getHeight()/2 -5, 10,10);
122 }
123 }
124
125
126
127
128
129
130
131
132 private void initComponents() {
133 eyeDropper = new javax.swing.JButton();
134 magPanel = new MagnifyingPanel();
135 activeColor = new JXColorSelectionButton();
136 hexColor = new javax.swing.JTextField();
137 jTextArea1 = new javax.swing.JTextArea();
138 jLabel1 = new javax.swing.JLabel();
139 rgbColor = new javax.swing.JTextField();
140 jLabel2 = new javax.swing.JLabel();
141
142 eyeDropper.setText("eye");
143
144 magPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
145 org.jdesktop.layout.GroupLayout magPanelLayout = new org.jdesktop.layout.GroupLayout(magPanel);
146 magPanel.setLayout(magPanelLayout);
147 magPanelLayout.setHorizontalGroup(
148 magPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
149 .add(0, 100, Short.MAX_VALUE)
150 );
151 magPanelLayout.setVerticalGroup(
152 magPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
153 .add(0, 100, Short.MAX_VALUE)
154 );
155
156 activeColor.setEnabled(false);
157 activeColor.setPreferredSize(new java.awt.Dimension(40, 40));
158
159 hexColor.setEditable(false);
160
161 jTextArea1.setColumns(20);
162 jTextArea1.setEditable(false);
163 jTextArea1.setLineWrap(true);
164 jTextArea1.setRows(5);
165 jTextArea1.setText("Drag the magnifying glass to select a color from the screen.");
166 jTextArea1.setWrapStyleWord(true);
167 jTextArea1.setOpaque(false);
168
169 jLabel1.setText("#");
170
171 rgbColor.setEditable(false);
172 rgbColor.setText("255,255,255");
173
174 jLabel2.setText("RGB");
175
176 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
177 this.setLayout(layout);
178 layout.setHorizontalGroup(
179 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
180 .add(layout.createSequentialGroup()
181 .addContainerGap()
182 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
183 .add(layout.createSequentialGroup()
184 .add(magPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
185 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
186 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
187 .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel1)
188 .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel2))
189 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
190 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
191 .add(org.jdesktop.layout.GroupLayout.LEADING, hexColor, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
192 .add(org.jdesktop.layout.GroupLayout.LEADING, rgbColor, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)
193 .add(activeColor, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)))
194 .add(layout.createSequentialGroup()
195 .add(eyeDropper)
196 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
197 .add(jTextArea1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)))
198 .addContainerGap())
199 );
200 layout.setVerticalGroup(
201 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
202 .add(layout.createSequentialGroup()
203 .addContainerGap()
204 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
205 .add(jTextArea1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 52, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
206 .add(eyeDropper))
207 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
208 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
209 .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
210 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
211 .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
212 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
213 .add(jLabel2)
214 .add(rgbColor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
215 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
216 .add(hexColor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
217 .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel1))
218 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 14, Short.MAX_VALUE)
219 .add(activeColor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
220 .add(magPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
221 .addContainerGap())
222 );
223 }
224
225
226
227 private javax.swing.JButton activeColor;
228 private javax.swing.JButton eyeDropper;
229 private javax.swing.JTextField hexColor;
230 private javax.swing.JLabel jLabel1;
231 private javax.swing.JLabel jLabel2;
232 private javax.swing.JTextArea jTextArea1;
233 private javax.swing.JPanel magPanel;
234 private javax.swing.JTextField rgbColor;
235
236
237 public void updateChooser() {
238 }
239
240 protected void buildChooser() {
241 }
242
243
244
245
246 public String getDisplayName() {
247 return "Grab from Screen";
248 }
249
250 public Icon getSmallDisplayIcon() {
251 return new ImageIcon();
252 }
253
254 public Icon getLargeDisplayIcon() {
255 return new ImageIcon();
256 }
257 }
258