如果屏幕上的颜色等于某个颜色,Java
我的部分程序需要查看屏幕上某个点的颜色是否等于某个颜色,然后执行一个操作。我不知道如何不断地检查屏幕上的点,看看颜色是否改变。任何帮助都将不胜感激如果屏幕上的颜色等于某个颜色,Java,java,colors,timer,screen,updating,Java,Colors,Timer,Screen,Updating,我的部分程序需要查看屏幕上某个点的颜色是否等于某个颜色,然后执行一个操作。我不知道如何不断地检查屏幕上的点,看看颜色是否改变。任何帮助都将不胜感激 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; jav
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不再等于1。提前谢谢
编辑:新的问题是我不能通过c发送来检查它是否是=to coal1
新代码(主体)
java.util.Timer
的方法可能由工作决定
关于您的最新帖子:你得走了
java.awt.Color c = robot.getPixelColor(xRock, yRock); // gets the color of the point
在TimerTask的run()方法中。由于它需要变量robot
、xRock
和yRock
,因此必须将它们作为参数传递给自定义构造函数。
例如:
“lookCoal()中的/*;方法*/
//schedule(t,0 2000)/一种方法是使用javascript。这篇文章可以帮助你做这件事
一旦能够获得所需的rgb值,就可以使用ajax请求或javascript提交在服务器上进行进一步处理。
HTH好的,但是现在我不能用c的run方法将变量c发送到类中,以与coal1进行比较。请用您迄今为止的尝试和遇到的问题更新您的答案,以便我们可以进一步提供帮助。当然可以。幸好你不需要它,所以你可以完全删除这一行:P我不会为你编写游戏代码。我给你主要的方向,但你必须解决细节:)非常感谢你到目前为止的帮助,这就是我想要的主要方向。这只是因为某种原因让我陷入困境。总是乐于助人:如果这个答案帮助你解决了你的问题,请考虑把它标记为“被接受”,所以将来面对类似问题的用户将能够很容易地发现它。
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.awt.Color c = robot.getPixelColor(xRock, yRock); // gets the color of the point
/* 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);
...