Java - get click off the limits of JPanel

451 Views Asked by At

I have the following doubt: Is it possible to get a "mouse left click event" off the limits of the component with a mouselistener? Or should i try this with another approach?

My problem is the following. I am creating a WYSIWYG panel that is suitable for my project. This panel is sibling to another panel that displays images that are loaded according to user selection. I need to get, for instance, the background color of this image. When any color is clicked this would change the bgcolor of the WYSIWYG panel. I am using the Robot class to get the color of a pixel, but this only works if the image and the color selector are in the same panel, but they won't be.

UPDATE: This code is what I mean. The mainframe has two independent JFrames. I need to get the rgb color of the images on the ImageLoader with a click on the MouseColorPane. On this case, the Robot can only get the black border of the JLabel.

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

public class WhatsMyColor {

    public static void main(String[] args) throws IOException {
        new WhatsMyColor();
    }

    public WhatsMyColor() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    final MouseColorPane mcp = new MouseColorPane();
                    frame.add(mcp,BorderLayout.CENTER);

                    ImageLoader il = new ImageLoader();
                    frame.add(il,BorderLayout.NORTH);
                    frame.setSize(800, 400);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);

                } catch (Exception exp) {
                    exp.printStackTrace();
                }

            }
        });
    }

    public class ImageLoader extends JPanel {

        ImageLoader(){
            BufferedImage img = null;
            try {img = ImageIO.read(new File("src/monkey-icon128.png"));} catch (IOException e1) {e1.printStackTrace();}
            add(new JLabel (new ImageIcon (img)));
            BufferedImage img2 = null;
            try {img2 = ImageIO.read(new File("src/monkey-icon128.png"));} catch (IOException e1) {e1.printStackTrace();}
            add(new JLabel (new ImageIcon (img2)));
        }
    }

    public class MouseColorPane extends JPanel {

        private Robot robot;
        private JLabel color;

        public MouseColorPane() throws AWTException {


            setLayout(new GridBagLayout());

            color = new JLabel();
            color.setBorder(BorderFactory.createLineBorder(Color.black));
            color.setPreferredSize(new Dimension(100,100));
            color.setFocusable(false);
            color.setOpaque(true);
            color.setFocusTraversalPolicyProvider(false);
            color.addMouseListener(new MouseListener() {
                @Override
                public void mouseReleased(MouseEvent arg0) {}
                @Override
                public void mousePressed(MouseEvent arg0) {}
                @Override
                public void mouseExited(MouseEvent arg0) {}
                @Override
                public void mouseEntered(MouseEvent arg0) {}
                @Override
                public void mouseClicked(MouseEvent arg0) {
                    // TODO Auto-generated method stub
                    PointerInfo pi;
                    pi = MouseInfo.getPointerInfo();
                    updateColor(pi.getLocation());
                }
            });
            add(color);

            robot = new Robot();
            setVisible(true);

        }

        protected void updateColor(Point p) {
            Color pixelColor = robot.getPixelColor(p.x, p.y);
            color.setBackground(pixelColor);       

        }


    }

}        
1

There are 1 best solutions below

0
On

You may get an idea of what's possible from Zoom, which uses Robot to capture a patch of the desktop and getRGB() to determine the color. Click and drag to capture; mouse over to see a tooltip containing the RGB components of a pixel.

image