2

Hello I am trying to get the color of a particular pixel on my JFrame.

This is my code. My frame is red.

The problem I am having is when I click the Frame it should return me the RGB color for red that is (255,0,0) but when I click at different points i sometimes get the RGB color for white (255,255,255) what is the problem in my code guys?

public class guiTest extends JFrame 
{

    private static Shape ellipse;   
     private static Robot rb;

    public guiTest()
    {
    super("4-connected approach");
    setLayout(new FlowLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    setSize(800,800);
    this.getContentPane().setBackground(Color.red);
    setLocationRelativeTo(null);
    addMouseListener(new MouseListener(){
        @Override
        public void mouseClicked(MouseEvent e) {
            System.out.println("Pixel:"+e.getX()+","+e.getY());             
            try {
                System.out.println(getPixel(e.getX(),e.getY()));
            } catch (AWTException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        }

        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub

        }


    });

}


public static Color getPixel(int x,int y) throws AWTException{
    Robot rb=new Robot();
    return rb.getPixelColor(x, y);
}   


public static void main(String[] args){
    guiTest frame=new guiTest();    
}
Java Man
  • 1,854
  • 3
  • 21
  • 43
user3323742
  • 31
  • 1
  • 4
  • You're not just returning white (255, 255, 255). Your method is returning a vast array of colours. I also get (30, 30, 30), (69, 70, 110), (45, 50, 72) and more besides. I'd say that's where your problem is (the `getPixel` method). Might also be a Swing issue with background colour vs. pixel approximation. – Gorbles Mar 06 '14 at 11:05
  • Also, why do you need the pixel colour? `this.getContentPane().getBackground()` returns the background colour set. If you render an image, you'd need to get the `Graphics` object and get the colour information from that. It all depends on what you're doing with it. – Gorbles Mar 06 '14 at 11:13
  • I am trying to get the pixel color because I want to implement the 8 connected boundary fill algorithm, any ways to grab the color of particular pixel accurately? – user3323742 Mar 07 '14 at 17:07
  • http://stackoverflow.com/questions/13307962/how-to-get-the-color-of-a-point-in-a-jpanel - might help you (see first upvoted answer). – Gorbles Mar 10 '14 at 14:42

1 Answers1

1

The problem is the way you are getting the coordinates - e.getX() and e.getY() -, because they are relative to the JFrame (the up-left corner of the JFrame is (0,0)).

To get the coordinates of the pixel, use:

public void mouseClicked(MouseEvent e) {
    Point p = e.getLocationOnScreen();

    System.out.println("Pixel:" + p.x + "," + p.y);
    try {
        System.out.println(getPixel(p.x, p.y));
    } catch (AWTException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

}

[Extra] read this to improve other things: Why is my mouse lagging when I run this small mouse hook application?

Community
  • 1
  • 1
D.Kastier
  • 2,640
  • 3
  • 25
  • 40