Search code examples
javacolorstimerscreenupdating

If the color on the screen equals a certain color, Java


Part of my program needs to see if the color at a certain point on the screen equals a certain color then preform an action. I can not figure out how to constantly check the point on the screen to see if that color changes. Any help would be greatly appreciated!

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!

}

I want it to keep looping until c no longer equals coal1. Thanks in advance!

EDIT: New problem is that I can not send through the c to check if it is = to coal1.

New Code (Main Body)

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

    }

}

Timer Class

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

Solution

  • java.util.Timer's schedule(TimerTask, long, long) method is probably up to the job.

    Regarding your updated post:
    You'll have to move

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

    inside the run() method of your TimerTask. And since it needs variables robot, xRock and yRock, you'll have to pass them as arguments to a custom constructor.
    E.g.:

    /* 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);
          ...