hi,

Recently i was trying to look for a pixel searcher for java that was built in with the language but the only command that was somewhat related to specific color pixel searching was the getPixelColor in the robot library. The problem with this command was that it only returned the color of the pixel that the x and y coordinates, that were entered into the method, point to. Anyways so i tried making something like it and here is my code:

import java.awt.Color;
import java.awt.Component;
import java.awt.Robot;
import java.awt.Toolkit;
import java.util.Scanner;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Peach implements Runnable {
    /// index
    // 0 is from divisor 1
    // 1 is from divisor 2
    // 2 is from divisor 3
    // 3 is from divisor 4
	
	int section; // to hold the array index number htingy
	
	public Peach (int divisor) {
		section = divisor;
		
	}
	

	  public void run() {
		  try {

		      Global_Variables gv = new Global_Variables();
			  Robot robot = new Robot();
			  Ease_Class ease = new Ease_Class();
			  
	 // divide screen dimensions set into four equal groups to be broken down
		// and worked on by four different threads
		 
		  	  
			  
			  
		  if (section == 0){
			  gv.min_x = gv.pos_x;
			  gv.min_y = gv.pos_y;
			  gv.max_x = (int) ((0.25 * gv.dimension_x) + gv.pos_x); 
			  gv.max_y = (int) ((0.50 * gv.dimension_y) + gv.pos_y);
			  
			  
		  }
		  else if (section == 1) {
			  gv.min_x = (int) ((0.25 * gv.dimension_x) + gv.pos_x);
			  gv.min_y = gv.pos_y;
			  gv.max_x = (int) ((0.50 * gv.dimension_x) + gv.pos_x); 
			  gv.max_y = (int) ((0.50 * gv.dimension_y) + gv.pos_y);
			  
		  }
		  else if (section == 2) {
			  gv.min_x = (int) ((0.50 * gv.dimension_x) + gv.pos_x);
			  gv.min_y = gv.pos_y;
			  gv.max_x = (int) ((0.75 * gv.dimension_x) + gv.pos_x); 
			  gv.max_y = (int) ((0.50 * gv.dimension_y) + gv.pos_y);
			  
		  }
		  else if (section == 3) {
			  gv.min_x = (int) ((0.75 * gv.dimension_x) + gv.pos_x);
			  gv.min_y = gv.pos_y;
			  gv.max_x = (int) ((1 * gv.dimension_x) + gv.pos_x); 
			  gv.max_y = (int) ((0.50 * gv.dimension_y) + gv.pos_y);
			  
		  }
		  
		  
		  
		  else if (section == 4) {
			  gv.min_x = gv.pos_x;
			  gv.min_y = (int) ((0.50 * gv.dimension_y) + gv.pos_y);
			  gv.max_x = (int) ((0.25 * gv.dimension_x) + gv.pos_x); 
			  gv.max_y = (int) (gv.dimension_y + gv.pos_y);
			  
		  }
		  else if (section == 5) {
			  gv.min_x = (int) ((0.25 * gv.dimension_x) + gv.pos_x);
			  gv.min_y = (int) ((0.50 * gv.dimension_y) + gv.pos_y);
			  gv.max_x = (int) ((0.50 * gv.dimension_x) + gv.pos_x); 
			  gv.max_y = (int) (gv.dimension_y + gv.pos_y);
			  
		  }
		  else if (section == 6) {
			  gv.min_x = (int) ((0.50 * gv.dimension_x) + gv.pos_x);
			  gv.min_y = (int) ((0.50 * gv.dimension_y) + gv.pos_y);
			  gv.max_x = (int) ((0.75 * gv.dimension_x) + gv.pos_x); 
			  gv.max_y = (int) (gv.dimension_y + gv.pos_y);
			  
		  }
		  else if (section == 7) {
			  gv.min_x = (int) ((0.75 * gv.dimension_x) + gv.pos_x);
			  gv.min_y = (int) ((0.50 * gv.dimension_y) + gv.pos_y);
			  gv.max_x = (int) ((1 * gv.dimension_x) + gv.pos_x); 
			  gv.max_y = (int) (gv.dimension_y + gv.pos_y);
			  
		  }
		  else {
			  ease.say_str("problem?!?");
		  }
		  
		  
	  boolean found = false, clicked = false;
          
      ease.say_float(gv.max_x);
      ease.say_float(gv.max_y);
      gv.x[section] = gv.min_x;
      gv.y[section] = gv.min_y;
      
      
      
      do {
    	//  ease.say_str("we r in the loop now w/ " + section);
    
    //	  ease.say_str("okay so the value for x and y are respectively");
    //	  ease.say_float(gv.x[section]);
    //	  ease.say_float(gv.y[section]);
      Color x1 = robot.getPixelColor(gv.x[section], gv.y[section]);
   //   System.out.println(x1);
      if (section == 2) {
        robot.mouseMove(gv.x[section], gv.y[section]); ///uncomment if u want to see mouse showing where is i

      }
 
      if (x1.equals(gv.Target_Color) || x1.equals(gv.Target_Color2) || x1.equals(gv.Target_Color3) ) {
         // ease.say_str("the following are x and y coords of found thingy");
         // ease.say_str("respectively");
         // ease.say_float(gv.x[section]);
         // ease.say_float(gv.y[section]);
          
          int checkersX = gv.x[section], checkersY = gv.y[section];
          int inspect[] = {0,0,0,0};
          

          // begining of directions checker
          
          checkersX = checkersX +1; // one more to left
          Color z1 = robot.getPixelColor(checkersX, checkersY);
          if (z1.equals(gv.Target_Color)) {
              ease.say_str("found it left one");
              checkersX = checkersX -1; // back to middle
              inspect[0] = 1;
              
          }
          else {
              checkersX = checkersX -1; // back to middle
              
          }
          
          
          checkersX = checkersX -1; // one more to right
          Color z2 = robot.getPixelColor(checkersX, checkersY);
          if (z2.equals(gv.Target_Color)) {
              ease.say_str("found it right one");
              checkersX = checkersX +1; // back to middle
              inspect[1] = 1;    
              
          }
          else {
              checkersX = checkersX +1; // back to middle
              
          }
          
          checkersY = checkersY +1; // one more to down
          Color z3 = robot.getPixelColor(checkersX, checkersY);
          if (z3.equals(gv.Target_Color)) {
              ease.say_str("found it down one");
              checkersY = checkersY -1; // back to middle
              inspect[2] = 1;
              
          }
          else {
              checkersY = checkersY -1; // back to middle
              
          }
          
          checkersY = checkersY -1; // one more to up
          Color z4 = robot.getPixelColor(checkersX, checkersY);
          if (z4.equals(gv.Target_Color)) {
              ease.say_str("found it up one");
              checkersY = checkersY +1; // back to middle
              inspect[3] = 1;
              
          }
          else {
              checkersY = checkersY +1; // back to middle
              
          }
          
          // end of directions checker
          // gv.x[section], gv.y[section]
          ease.say_str("This is thread #" + section +" reporting.");
          ease.say_str("Found @ x " + gv.x[section] + " and y " + gv.y[section]);
          
          if (clicked == false) {
        	  robot.mouseMove(gv.x[section], gv.y[section]);
        	  robot.mousePress(InputEvent.BUTTON1_MASK);
        	  robot.mouseRelease(InputEvent.BUTTON1_MASK);
        	  clicked = true;
          }
          
          
         
  //        found = true;    // take off or on to set loop to stop or not when found
          
          
          for (int i = 0; i < 4; i++) {
              gv.results[section] = gv.results[section] + inspect[i];
          }
          
          // direction result reporter
          if (gv.results[section] == 4) {
              ease.say_str("found in all four directions");
          }
          else if (gv.results[section] == 3) {
              ease.say_str("found in 3 directions");
          }
          else if (gv.results[section] == 2) {
              ease.say_str("found in 2 directions");
          }
          else if (gv.results[section] == 1) {
              ease.say_str("found in 1 directions");
          }
          // end of direction result reporter
              
      }
          
      else {
      //ease.say_str("Not found...");
      clicked = false;
    	  
      gv.x[section] = gv.x[section] + gv.how_much_left;
  //    ease_ease.say_str("x went up one and turn of x is");
  //    System.out.println(turn_of_x);
          
      if (gv.x[section] >= gv.max_x) {

          gv.y[section] = gv.y[section] + gv.how_much_down;
          gv.x[section] = gv.min_x;
          
          if (gv.y[section] >= gv.max_y) {

              gv.y[section] = gv.min_y;   // will not search last pixel row
              gv.x[section] = gv.x[section] + 1; // this will cause the searcher to 
      // start 1 ahead next time it starts which will mean it will search
      // different pixels every time,  depending on how much left
          ease.say_str("theorectically restarting search in threead " + section);
          }
      }
          
          
         
      }// closing brac for else
      
      }while (!found);
      
		  } catch (Exception ex) {
	            System.out.println(ex);
	        }

	    }
	    	
	   

}

As you can see the above class uses objects from the global variable class which is shown below:

public int [] x = new int[8];
	public int [] y = new int[8];
	public int [] results = new int [8];
	public Color Target_Color = new Color(95, 78, 59);
	public Color Target_Color2 = new Color(57, 51, 35);
	public Color Target_Color3 = new Color(104, 94, 64); // color to be searched for
	public int how_much_down = 4;   /// in actuality its how much down
	public int how_much_left = 4; // as in opposite to right
	public int screen_x = 550, screen_y = 350;   /// max side values for the screen 
												/// being tested on 
	public int pos_x = 252, pos_y = 204; // position where search is gunna begin
	
	public int dimension_x = 550, dimension_y = 350;
	
	// generalizing the thread 
	public int max_x;
	public int max_y;
	public int min_x;
	public int min_y;

The class was meant to be used in different threads so as to make the searching more efficient by breaking down the area to be worked with into 8 different sections.
The problem i came across was that when I used the mouseMove method to show how position of the pixels being searched i found out that when I ran just one thread with the mouseMove method the mouse moved at an acceptable speed as the pixels were being searched, but when I ran it with 7 other threads and made only one thread use the mouseMove method i found that the speed of the search declined greatly even though my computer was functioning at normal speed. Obviously with it functioning at such a slow speed, it cant be dependable. What i wanted to know was if there was a better way to search for a specific pixel color on an area on the screen or if there was a way to make my function more efficient. Thanks in advance for the help.

btw, the code might not be in the best form, I am still learning java.

Recommended Answers

All 3 Replies

Using extra threads without the extra cores will slow down the program.
Searching an x by y rectangle for a match could take up to x*y gets to find it.

How would one go about adding more cores, if it is possible?

That was the only way i could think of looking for a color pixel on a screen given the code I had that was given through the robot class. I did get the program to skip over a few pixels in order to make the program go faster.

How would one go about adding more cores

Buy a new cpu chip and motherboard for your PC.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.