5

I have a problem with finding the current color under the cursor.

My code:

import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;

public class Test {
    public static void main(String[] args) throws Exception {
        PointerInfo pointer;
        pointer = MouseInfo.getPointerInfo();
        Point coord = pointer.getLocation();

        Robot robot = new Robot();
        robot.delay(2000);

        while(true) {
            coord = MouseInfo.getPointerInfo().getLocation();       
            Color color = robot.getPixelColor((int)coord.getX(), (int)coord.getX());
            if(color.getGreen() == 255 && color.getBlue() == 255 && color.getRed() == 255) {
                System.out.println("WHITE FOUND");
            }
            robot.delay(1000);
        }
    }
}

When I run it, even when I hold my mouse on the gray area, I am getting “WHITE FOUND WHITE FOUND” message.

What can be the problem? Can you guys test if it does not work for you also?

Added Picture: I am holding my cursor on Eclipse gray area but getting “WHITE FOUND” message.

enter image description here

Abbas
  • 6,720
  • 4
  • 35
  • 49
Jaanus
  • 16,161
  • 49
  • 147
  • 202

3 Answers3

6

I think the problem is you are using getX twice instead of getX and getY

Color color = robot.getPixelColor((int)coord.getX(), (int)coord.getX())

Should be

Color color = robot.getPixelColor((int)coord.getX(), (int)coord.getY())
MBU
  • 4,998
  • 12
  • 59
  • 98
2

You're using getX() twice. [min length]

ronash
  • 856
  • 1
  • 9
  • 15
0

You might also like Zoom, which uses the related method createScreenCapture() to collect screen pixels en masse and display color information about each in a tool tip.

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