0

パズルを解くためのJavaアプリケーションを構築しています。私が基本的にコーディングしている方法では、プログラムはスクリーンショットを撮り、スクリーンショットでピクセルを見つけ、ロボット機能を介してマウスをデスクトップ上のその位置に移動します。スクリーンショットを撮り、それを配列に保存し、適切な色の組み合わせで保存されたピクセルがポップアップするまで配列を探索し、マウスを画面上のその位置に移動するという理論を理解していますが、私の人生では取得できませんコードダウン。スクリーン ショットを取得し、それを配列に保存するサンプル コードを誰かが知っている場合 (または、この特定の用途に配列が最適かどうかはわかりません)、その配列からピクセルを見つけてマウスを移動します。ピクセル位置に移動してから配列をクリアすると、驚くほど満腹になります。これは私を夢中にさせるからです!

これまでのところ、私は持っています:

public static void main(String[] args) throws Exception{

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;
            }
        }
    }
}

}

私は3つのエラーが発生しています:

  1. 不正な式の開始、以下の get in コード セグメントを指すインジケータ

    private static Rectanglerectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());

  2. 不正な式の開始、以下のコード セグメントのサイズを指しているインジケーター

    private static Rectanglerectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());

  3. ; Rectangle 長方形を指している予想されるインジケーター

    private static Rectanglerectangle = new 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 に答える