Color == Robot.getPixelColor comes out as false even though they are the same color?

Viewed 29

I am a beginner at java so please forgive my ignorance. For a class project, I'm wanting to find out if a pixel on my JFrame is the same color as a predetermined color. I used Robot.getPixelColor to find out the color on the JFrame. The problem I'm having is is that despite the pixel color and the predetermined color being the same, the boolean comes out as false if I compare them. Thank you so much for reading!

JFrame frame = new JFrame(); frame.setSize(500,500);

      frame.setBackground(Color.WHITE);;
      Color backgroundColor = null;
        try {
            backgroundColor = new Robot().getPixelColor(82,355);
        } catch (AWTException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        Color color = Color.WHITE;
        System.out.println(backgroundColor);
        System.out.println(color);
        System.out.println(backgroundColor == color);
        
        frame.setVisible(true);
1 Answers

you can't use == operator on objects (it will compare only the object addresses).
try (backgroundColor.equals(color))

Related