Search code examples
javaawtrobot

how to look for a colour and click on it (white colour keeps changing shades) Java


import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.awt.Color;


public class main {
  private static Robot robot = null;

  public static void main(String[] args) 
  {
    try {
      robot = new Robot ();
    } catch (AWTException e) {
      e.printStackTrace();
    }

    klick (700 , 118);
    robot.delay(5000);
    colour(700,118);
  }


  public static void klick ( int x , int y)
  {
    robot.mouseMove(x, y);
    robot.delay(5);
    robot.mousePress(MouseEvent.BUTTON1_MASK);
    robot.mouseRelease(MouseEvent.BUTTON1_MASK);
  }
  public static void colour (int x, int y)
  {
    robot.delay(5);
    Color color = robot.getPixelColor(x,y);
    robot.delay(5);
    System.out.println("Red   = " + color.getRed());
    System.out.println("Green = " + color.getGreen());
    System.out.println("Blue  = " + color.getBlue());

  }
}

i want it to find white anywhere on the screen and click it this is what i have so far. STEP 1: CLICK ON FIXED POINT STEP 2: LOOK FOR WHITE COLOUR STEP 3: CLICK ON WHITE COLOUR thats all i need help with right now


Solution

  • I don't really understand your question but if you're trying to get the white color to become shaded when you click it (pixel at a time) then you could use this to darken the pixel:

    public BufferedImage shadePixel(BufferedImage img, int x, int y, int darkness){
    //x is the mouse x position & y is the mouse y position
    
    Color color = new Color(img.getRGB(x, y));
    int imgR = color.getRed()-darkness;//if it makes it brighter try + instead of -
    int imgG = color.getGreen()-darkness;
    int imgB = color.getBlue()-darkness;
    Color color2 = new Color(imgR, imgG, imgB);
    
    img.setRGB(x, y, color2.getRGB());
    
    return img;
    }
    

    Hope it helps :)

    But if you meant for it to detect the different shades then you could try this:

    public boolean isShadeOfWhite(BufferedImage img, int x, int y){
    Color color = new Color(img.getRGB(x, y));
    int imgR = color.getRed();
    int imgG = color.getGreen();
    int imgB = color.getBlue();
    
    if(imgR == imgG && imgR == imgB){
        return true;
    }
        return false;
    }