001/*
002 * $Id: EyeDropperColorChooserPanel.java 4082 2011-11-15 18:39:43Z kschaefe $
003 *
004 * Copyright 2008 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020 */
021package org.jdesktop.swingx.color;
022
023import java.awt.AWTException;
024import java.awt.Color;
025import java.awt.Graphics;
026import java.awt.Point;
027import java.awt.Rectangle;
028import java.awt.Robot;
029import java.awt.event.MouseEvent;
030import java.awt.geom.Point2D;
031import java.awt.image.BufferedImage;
032import java.beans.PropertyChangeEvent;
033import java.beans.PropertyChangeListener;
034
035import javax.swing.Icon;
036import javax.swing.ImageIcon;
037import javax.swing.JColorChooser;
038import javax.swing.JFrame;
039import javax.swing.JLabel;
040import javax.swing.JPanel;
041import javax.swing.JTextArea;
042import javax.swing.SwingUtilities;
043import javax.swing.colorchooser.AbstractColorChooserPanel;
044import javax.swing.event.MouseInputAdapter;
045
046import org.jdesktop.swingx.JXColorSelectionButton;
047import org.jdesktop.swingx.util.PaintUtils;
048
049/**
050 * <p>EyeDropperColorChooserPanel is a pluggable panel for the 
051 * {@link JColorChooser} which allows the user to grab any 
052 * color from the screen using a magnifying glass.</p>
053 *
054 * <p>Example usage:</p>
055 * <pre><code>
056 *    public static void main(String ... args) {
057        SwingUtilities.invokeLater(new Runnable() {
058            public void run() {
059                JColorChooser chooser = new JColorChooser();
060                chooser.addChooserPanel(new EyeDropperColorChooserPanel());
061                JFrame frame = new JFrame();
062                frame.add(chooser);
063                frame.pack();
064                frame.setVisible(true);
065            }
066        });
067    }
068 * </code></pre>
069 *
070 * @author joshua@marinacci.org
071 */
072public class EyeDropperColorChooserPanel extends AbstractColorChooserPanel {
073
074    /**
075     * Example usage
076     */
077    public static void main(String ... args) {
078        SwingUtilities.invokeLater(new Runnable() {
079            public void run() {
080                JColorChooser chooser = new JColorChooser();
081                chooser.addChooserPanel(new EyeDropperColorChooserPanel());
082                JFrame frame = new JFrame();
083                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
084                frame.add(chooser);
085                frame.pack();
086                frame.setVisible(true);
087            }
088        });
089    }
090    
091    /**
092     * Creates new EyeDropperColorChooserPanel
093     */
094    public EyeDropperColorChooserPanel() {
095        initComponents();
096        MouseInputAdapter mia = new MouseInputAdapter() {
097            @Override
098            public void mousePressed(MouseEvent evt) {
099            }
100            @Override
101            public void mouseDragged(MouseEvent evt) {
102                Point pt = evt.getPoint();
103                SwingUtilities.convertPointToScreen(pt,evt.getComponent());
104                ((MagnifyingPanel)magPanel).setMagPoint(pt);
105            }
106            @Override
107            public void mouseReleased(MouseEvent evt) {
108                Color newColor = new Color(((MagnifyingPanel)magPanel).activeColor);
109                getColorSelectionModel().setSelectedColor(newColor);
110            }
111        };
112        eyeDropper.addMouseListener(mia);
113        eyeDropper.addMouseMotionListener(mia);
114        try {
115            eyeDropper.setIcon(new ImageIcon(
116                    EyeDropperColorChooserPanel.class.getResource("mag.png")));
117            eyeDropper.setText("");
118        } catch (Exception ex) {
119            ex.printStackTrace();
120        }
121        
122        magPanel.addPropertyChangeListener(new PropertyChangeListener() {
123            public void propertyChange(PropertyChangeEvent evt) {
124                Color color = new Color(((MagnifyingPanel)magPanel).activeColor);
125                activeColor.setBackground(color);
126                hexColor.setText(PaintUtils.toHexString(color).substring(1));
127                rgbColor.setText(color.getRed() +"," + color.getGreen() + "," + color.getBlue());
128            }
129        });
130    }
131    
132    private class MagnifyingPanel extends JPanel {
133        private Point2D point;
134        private int activeColor;
135        public void setMagPoint(Point2D point) {
136            this.point = point;
137            repaint();
138        }
139        @Override
140        public void paintComponent(Graphics g) {
141            if(point != null) {
142                Rectangle rect = new Rectangle((int)point.getX()-10,(int)point.getY()-10,20,20);
143                try {
144                    BufferedImage img =new Robot().createScreenCapture(rect);
145                    g.drawImage(img,0,0,getWidth(),getHeight(),null);
146                    int oldColor = activeColor;
147                    activeColor = img.getRGB(img.getWidth()/2,img.getHeight()/2);
148                    firePropertyChange("activeColor", oldColor, activeColor);
149                } catch (AWTException ex) {
150                    ex.printStackTrace();
151                }
152            }
153            g.setColor(Color.black);
154            g.drawRect(getWidth()/2 - 5, getHeight()/2 -5, 10,10);
155        }
156    }
157    
158    
159    /** This method is called from within the constructor to
160     * initialize the form.
161     * WARNING: Do NOT modify this code. The content of this method is
162     * always regenerated by the Form Editor.
163     */
164    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
165    private void initComponents() {
166        java.awt.GridBagConstraints gridBagConstraints;
167
168        eyeDropper = new javax.swing.JButton();
169        magPanel = new MagnifyingPanel();
170        activeColor = new JXColorSelectionButton();
171        hexColor = new javax.swing.JTextField();
172        JTextArea jTextArea1 = new JTextArea();
173        jLabel1 = new javax.swing.JLabel();
174        rgbColor = new javax.swing.JTextField();
175        JLabel jLabel2 = new JLabel();
176
177        setLayout(new java.awt.GridBagLayout());
178
179        eyeDropper.setText("eye");
180        add(eyeDropper, new java.awt.GridBagConstraints());
181
182        magPanel.setLayout(new java.awt.BorderLayout());
183
184        magPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
185        magPanel.setMinimumSize(new java.awt.Dimension(100, 100));
186        magPanel.setPreferredSize(new java.awt.Dimension(100, 100));
187        gridBagConstraints = new java.awt.GridBagConstraints();
188        gridBagConstraints.gridx = 0;
189        gridBagConstraints.gridy = 1;
190        gridBagConstraints.gridheight = 3;
191        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
192        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 12);
193        add(magPanel, gridBagConstraints);
194
195        activeColor.setEnabled(false);
196        activeColor.setPreferredSize(new java.awt.Dimension(40, 40));
197        gridBagConstraints = new java.awt.GridBagConstraints();
198        gridBagConstraints.gridx = 2;
199        gridBagConstraints.gridy = 3;
200        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
201        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
202        add(activeColor, gridBagConstraints);
203
204        hexColor.setEditable(false);
205        gridBagConstraints = new java.awt.GridBagConstraints();
206        gridBagConstraints.gridx = 2;
207        gridBagConstraints.gridy = 1;
208        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
209        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
210        add(hexColor, gridBagConstraints);
211
212        jTextArea1.setColumns(20);
213        jTextArea1.setEditable(false);
214        jTextArea1.setLineWrap(true);
215        jTextArea1.setRows(5);
216        jTextArea1.setText("Drag the magnifying glass to select a color from the screen.");
217        jTextArea1.setWrapStyleWord(true);
218        jTextArea1.setOpaque(false);
219        gridBagConstraints = new java.awt.GridBagConstraints();
220        gridBagConstraints.gridwidth = 2;
221        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
222        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
223        gridBagConstraints.weightx = 10.0;
224        gridBagConstraints.weighty = 10.0;
225        gridBagConstraints.insets = new java.awt.Insets(0, 0, 7, 0);
226        add(jTextArea1, gridBagConstraints);
227
228        jLabel1.setText("#");
229        gridBagConstraints = new java.awt.GridBagConstraints();
230        gridBagConstraints.gridx = 1;
231        gridBagConstraints.gridy = 1;
232        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
233        gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
234        add(jLabel1, gridBagConstraints);
235
236        rgbColor.setEditable(false);
237        rgbColor.setText("255,255,255");
238        gridBagConstraints = new java.awt.GridBagConstraints();
239        gridBagConstraints.gridx = 2;
240        gridBagConstraints.gridy = 2;
241        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
242        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
243        add(rgbColor, gridBagConstraints);
244
245        jLabel2.setText("RGB");
246        gridBagConstraints = new java.awt.GridBagConstraints();
247        gridBagConstraints.gridx = 1;
248        gridBagConstraints.gridy = 2;
249        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
250        gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
251        add(jLabel2, gridBagConstraints);
252
253    }// </editor-fold>//GEN-END:initComponents
254    
255    
256    // Variables declaration - do not modify//GEN-BEGIN:variables
257    private javax.swing.JButton activeColor;
258    private javax.swing.JButton eyeDropper;
259    private javax.swing.JTextField hexColor;
260    private javax.swing.JLabel jLabel1;
261    private javax.swing.JPanel magPanel;
262    private javax.swing.JTextField rgbColor;
263    // End of variables declaration//GEN-END:variables
264    
265    /**
266     * {@inheritDoc}
267     */
268    @Override
269    public void updateChooser() {
270    }
271    
272    /**
273     * {@inheritDoc}
274     */
275    @Override
276    protected void buildChooser() {
277    }
278    
279    /**
280     * {@inheritDoc}
281     */
282    @Override
283    public String getDisplayName() {
284        return "Grab from Screen";
285    }
286    
287    /**
288     * {@inheritDoc}
289     */
290    @Override
291    public Icon getSmallDisplayIcon() {
292        return new ImageIcon();
293    }
294    
295    /**
296     * {@inheritDoc}
297     */
298    @Override
299    public Icon getLargeDisplayIcon() {
300        return new ImageIcon();
301    }
302}