0

我正在构建一个 java 应用程序来解决难题。我编写它的方式基本上程序将截取屏幕截图,在屏幕截图中找到一个像素,然后通过机器人功能将鼠标移动到桌面上的那个位置。我了解拍摄屏幕截图、将其存储在数组中、探索数组直到弹出具有正确颜色组合的存储像素并将鼠标移动到屏幕上的该位置的理论,但是我终生无法获得码下来。如果有人知道,或者可以拼凑一个截屏的示例代码,将其存储在一个数组中(或者存储设备我不知道数组是否最适合这种特定用途)从该数组中找到一个像素移动鼠标到像素位置然后清除阵列,我会非常满意,因为这让我发疯!

到目前为止,我有:

公共静态 void main(String[] args) 抛出异常 {

Robot robot = new Robot();

{
private static Rectangle rectangle = new Rectangle(0, 0, 1075, 700);

{
    BufferedImage image = r.createScreenCapture(rectangle);
    search: for(int x = 0; x < rectangle.getWidth(); x++)
    {
        for(int y = 0; y < rectangle.getHeight(); y++)
        {
            if(image.getRGB(x, y) == Color.getRGB(195, 174, 196))
            {
                Robot.mouseMove(x, y);
                break search;
            }
        }
    }
}

}

我收到三个错误:

  1. 表达式的非法开始,指示符指向下面的代码段

    私有静态矩形矩形 = 新矩形(Toolkit.getDefaultToolkit().getScreenSize());

  2. 表达式开头非法,下面代码段中指向 Size 的指标

    私有静态矩形矩形 = 新矩形(Toolkit.getDefaultToolkit().getScreenSize());

  3. ; 预期指标指向 Rectangle 矩形

    私有静态矩形矩形 = 新矩形(Toolkit.getDefaultToolkit().getScreenSize());

4

1 回答 1

1

Creating the screen shot and looping though it is not that hard. The Javadoc for the GraphicsDevice will tell you how to get the right screen size.

The only thing I don't think you can do is respond to "color events". You can poll the screen to see when the color has changed though.

import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;

public class FindColor
{
    private static Rectangle rectangle = new Rectangle(800, 600);

    public static void main(String[] args) throws Exception
    {
        Robot r = new Robot();
        BufferedImage image = r.createScreenCapture(rectangle);
        search: for(int x = 0; x < rectangle.getWidth(); x++)
        {
            for(int y = 0; y < rectangle.getHeight(); y++)
            {
                if(image.getRGB(x, y) == Color.BLACK.getRGB())
                {
                    r.mouseMove(x, y);
                    System.out.println("Found!");
                    break search;
                }
            }
        }
    }
}

-edit since the question was expanded- You don't need to write the image out to disk if you are going to check it there and then. The BufferedImage already has a way to access the individual pixels so I don't think there is a need to translate the pixel data into an array.

于 2012-01-31T05:23:44.733 回答