Часть моей программы должна видеть, соответствует ли цвет в определенной точке на экране определенным цветом, а затем преформирует действие. Я не могу понять, как постоянно проверять точку на экране, чтобы увидеть, изменился ли этот цвет. Любая помощь будет принята с благодарностью!Если цвет на экране равен определенному цвету, Java
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. Заранее спасибо!
EDIT: Новая проблема заключается в том, что я не могу отправить через c, чтобы проверить, является ли это = для угля1.
Новый код (основной корпус)
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();
}
}
}
Хорошо, но теперь я не могу отправить переменную c классу с помощью метода run для c для сравнения с coal1. – MahoreLee
Обновите свой ответ тем, что вы пробовали, и где вы застряли, поэтому мы можем помочь вам. – gkalpak
Хорошо, я обновил его. – MahoreLee