【问题标题】:If the color on the screen equals a certain color, Java如果屏幕上的颜色等于某种颜色,Java
【发布时间】:2013-05-24 15:30:26
【问题描述】:

我的程序的一部分需要查看屏幕上某个点的颜色是否等于某个颜色,然后执行一个动作。我不知道如何不断检查屏幕上的点以查看颜色是否发生变化。任何帮助将不胜感激!

static Color coal1 = new Color(19, 19, 18); //color i want to match up    

int xRock = gameSquare.x + x_scale; // points on the screen
int yRock = gameSquare.y + y_scale;

java.awt.Color c = robot.getPixelColor(xRock, yRock);//finds the rgb color of the point

if (!c.equals(coal1)){ //this is the part where I am stuck!

}

我希望它继续循环,直到 c 不再等于 coal1。提前致谢!

编辑:新问题是我无法通过 c 发送以检查它是否 = 到 coal1。

新代码(主体)

package Restart;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferInt;
import java.awt.image.WritableRaster;
import java.util.Timer;

public class MineCoal {

    public static Rectangle gameSquare;
    public static boolean runningMine = true;
    static Timer timer = new Timer("Printer");
    static MyTask t = new MyTask();

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

        lookCoal();

    }

    public static void lookCoal() throws InterruptedException{

        Thread.sleep(2000);
        try {
            Robot robot = new Robot();

            while(runningMine == true){

                gameSquare = new Rectangle(287,139,551,356); //== game square
                BufferedImage img = robot.createScreenCapture(gameSquare);
                WritableRaster r = img.getRaster();
                DataBuffer db = r.getDataBuffer();
                DataBufferInt dbi = (DataBufferInt)db;
                int[] data = dbi.getData();                 

                for (int x_scale = 0; x_scale < gameSquare.width; x_scale += 1) {
                    for(int y_scale = 0; y_scale < gameSquare.height; y_scale += 1) {
                        int rgb = data[x_scale + gameSquare.width * y_scale];

                        if ( (rgb == -15658737)||(rgb ==-15527150) ){ //finds coal
                            //checkinv();
                            if (runningMine == true){

                                runningMine = false;

                                int xRock = gameSquare.x + x_scale; // sets the x and y of the point
                                int yRock = gameSquare.y + y_scale;
                                robot.mouseMove(xRock, yRock);
                                java.awt.Color c = robot.getPixelColor(xRock, yRock); // gets the color of the point
                                System.out.println(c);

                                Thread.sleep(500);
                                robot.mousePress(InputEvent.BUTTON1_MASK);
                                robot.mouseRelease(InputEvent.BUTTON1_MASK);
                                Thread.sleep(1000);
                                System.out.println("Found Rock");

                                timer.schedule(t, 0, 2000); //this goes to check if the point has changed colors but I can not send c through

                            }

                        }

                    }
                }
            }
        }
        catch(AWTException e) {
            e.printStackTrace();
        }

    }

}

定时器类

package Restart;
import java.awt.Color;
import java.util.TimerTask;

class MyTask extends TimerTask {
    //times member represent calling times.
    private int times = 0;
    static Color coal1 = new Color(19, 19, 18);


    public void run() {

        if (coal1.equals(c)) {  //I can not get the c from the other class to compair with coal1
            System.out.println("color is the same");
        } 
        else {
            System.out.println("color changed");

            //Stop Timer.
            this.cancel();
        }
    }
}

【问题讨论】:

    标签: java colors timer screen updating


    【解决方案1】:

    java.util.Timerschedule(TimerTask, long, long) 方法可能适合这项工作。

    关于您更新的帖子:
    你必须搬家

    java.awt.Color c = robot.getPixelColor(xRock, yRock); // gets the color of the point
    

    在 TimerTask 的 run() 方法中。由于它需要变量robotxRockyRock,因此您必须将它们作为参数传递给自定义构造函数。
    例如:

    /* In 'lookCoal(); method */
      // timer.schedule(t, 0 2000)   // <-- replace that with this:
      timer.schedule(new MyTask(robot, xRock, yRock), 0, 2000);
    
    /* In 'MyTask' Class */
      // Add the required variables:
      Robot robot;
      int xRock, yRock;
    
      // Add a constructor like this:
      MyTask(Robot r, int x, int y) {
          this.robot = r;
          this.xRock = x;
          this.yRock = y;
      }
    
      // Modify the run method, like this:
      public void run() {
          java.awt.Color c = this.robot.getPixelColor(this.xRock, this.yRock);
          ...
    

    【讨论】:

    • 好的,但是现在我不能将变量 c 发送到使用 run 方法让 c 与 coal1 进行比较的类。
    • 用您目前尝试过的内容和遇到的问题更新您的答案,以便我们进一步提供帮助。
    • 当然可以。好在你不需要那个,所以你可以完全删除这条线:P 我不会为你编写那个游戏的代码。我给你主要方向,但你必须弄清楚细节:)
    • 非常感谢您到目前为止的帮助,这就是我想要的主要方向。出于某种原因,这只是让我陷入困境。
    • 总是乐于提供帮助 :) 如果这个答案帮助您解决了您的问题,请考虑将其标记为“已接受”,以便将来遇到类似问题的用户能够轻松发现它。
    【解决方案2】:

    一种方法是使用 javascript。这篇文章可以帮助你如何做到这一点https://stackoverflow.com/a/11506531/1083581

    一旦您能够获得所需的 rgb 值,您就可以使用 ajax 请求或 javascript 提交,以便在服务器上进行进一步处理。
    高温

    【讨论】:

      猜你喜欢
      • 2013-05-20
      • 2012-07-23
      • 2012-05-16
      • 1970-01-01
      • 2014-04-08
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      相关资源
      最近更新 更多