0

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);       

        }


    }

}        
pcezar91
  • 111
  • 11
  • 2
    If your concern is the mouse click within the Application's Dimensions, then yes it is possible. You simply have to apply the same logic, for the second `JPanel`, though how exactly you going about it, is a mystery without actually looking at your code. So please provide one small, short, correct, compilable example, a [SSCCE](http://sscce.org/) :-) – nIcE cOw Aug 26 '13 at 16:31
  • 2
    Wouldn't just adding the listener to the panel that you're going to be clicking work? That makes more sense than putting it on the panel that you're not clicking. – Cruncher Aug 26 '13 at 16:34
  • 2
    Why using the `Robot` class? Since you are loading the images you shouldn't you be able to get the color from the image directly? Please post the relevant code – c.s. Aug 26 '13 at 18:52
  • c.s. the images vary a lot, and i need to get a specific part of the image. – pcezar91 Aug 27 '13 at 18:14

1 Answers1

2

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

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045