FileDocCategorySizeDatePackage
ColorChooserDemo.javaAPI DocExample3493Mon Jan 09 11:02:00 GMT 2006None

ColorChooserDemo

public class ColorChooserDemo extends JFrame implements MouseListener, MouseMotionListener

Fields Summary
JPanel
image_panel
Dimension
screen_size
JComponent
comp
Image
background_image
Robot
robot
JLabel
label
Constructors Summary
public ColorChooserDemo(JComponent comp)

    
       
        // get the screen dimensions
        screen_size = Toolkit.getDefaultToolkit().getScreenSize();
        
        // setup the frame (this)
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
        this.comp = comp;
        this.setUndecorated(true);
        this.setSize(screen_size.width, screen_size.height);
        
        // set up the panel that holds the screenshot
        image_panel = new JPanel() {
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(background_image,0,0,null);
            }
        };
        image_panel.setPreferredSize(screen_size);
        image_panel.setLayout(null);
        this.getContentPane().add(image_panel);
        
        // setup the display label
        label = new JLabel("Selected Color");
        label.setOpaque(true);
        label.setSize(100,100);
        image_panel.add(label);
        label.setLocation((int)screen_size.getWidth()/2 - 50,
            (int)screen_size.getHeight()/2 - 50);
    
Methods Summary
public static voidmain(java.lang.String[] args)

        JFrame frame = new JFrame("Color Chooser Hack");
        final JButton button = new JButton("Click to choose a color");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                JFrame frame = new ColorChooserDemo(button);
                frame.show();
            }
        });
        
        frame.getContentPane().add(button);
        frame.pack();
        frame.setVisible(true);
    
public voidmouseClicked(java.awt.event.MouseEvent evt)

 
public voidmouseDragged(java.awt.event.MouseEvent evt)

        setSelectedColor(robot.getPixelColor(evt.getX(), evt.getY()));
    
public voidmouseEntered(java.awt.event.MouseEvent evt)

 
public voidmouseExited(java.awt.event.MouseEvent evt)

 
public voidmouseMoved(java.awt.event.MouseEvent evt)

 
public voidmousePressed(java.awt.event.MouseEvent evt)

        setSelectedColor(robot.getPixelColor(evt.getX(), evt.getY()));
    
public voidmouseReleased(java.awt.event.MouseEvent evt)

        setSelectedColor(robot.getPixelColor(evt.getX(),evt.getY()));
        this.setVisible(false);
    
public voidsetSelectedColor(java.awt.Color color)

        comp.setBackground(color);
        label.setBackground(color);
    
public voidshow()

        try {
            // make the screenshot before showing the frame
            Rectangle rect = new Rectangle(0,0,
                 (int)screen_size.getWidth(),
                 (int)screen_size.getHeight());
            this.robot = new Robot();
            background_image = robot.createScreenCapture(rect);
            super.show();
        } catch (AWTException ex) {
            System.out.println("exception creating screenshot:");
            ex.printStackTrace();
        }