Здесь проверьте код ниже. У вас было две ошибки проверки комментариев.
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
public class App {
public static Point pixelSearchArea(String hexColor, Point topLeft, Point bottomRight) throws AWTException{
int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
// int areaWidth = bottomRight.y - topLeft.y; - for width use X
int areaWidth = bottomRight.x - topLeft.x;
int areaHeight = bottomRight.y - topLeft.y;
Rectangle area = new Rectangle(topLeft.x, topLeft.y, areaWidth, areaHeight);
Color colorColor = Color.decode(hexColor);
int searchColor = colorColor.getRGB();
Robot r = new Robot();
Rectangle screenRect = area;
BufferedImage bimage = r.createScreenCapture(screenRect);
int width = bimage.getWidth();
int height = bimage.getHeight();
// int[] colors = new int[width * height]; - read the docs.
int[] all = bimage.getRGB(0, 0, width, height, null, 0, width);
for(int i = 0; i < all.length; i++)
{
if (all[i] == searchColor)
{
int y = ((i + 1) / width);
int x = (i) - (y * width);
return new Point(screenWidth - (screenWidth - topLeft.x) + x,screenHeight - (screenHeight - topLeft.y) + y);
}
}
return null;
}
public static void main(String[] args) {
try {
// It will set the mouse pointer to the last white pixel
Point p = pixelSearchArea("#FFFFFF", new Point(0, 0), new Point(500, 500));
Robot r = new Robot();
r.mouseMove(p.x, p.y);
} catch (AWTException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Прочитайте документы для BufferedImage # getRGB () .