Java Robot class mouse move to position of a specific pixel (Mouse click a color)

14,763

Solution 1

You'll most likely need to take a screen shot image then spiral out (assuming the "color" is taking a continuous path and not jumping around) from the original location comparing the color of that pixel with the color you are looking for. Once that has been identified, do mouseMove(newX, newY) and then the mousePress()/mouseRelease() methods.

Solution 2

if the color is taking continues path and not jumping around read Kevin Mangold answer otherwise if it is just a color appearing anywhere you I think you have 2 options(in case of the background being constant color):

The First one: you can take screen shot iterate over it and get coordinates of any appearing color (or a specific color)then press on it using Robot lib,this may help and this for taking screenshots

The second one: if you don't want to take screenshots you can use the robot lib to iterate over your screen with a 2 nested for loops iterating over all screen pixels THis may help

In case of background image is not constant you can take screenshot of the screen and compare it with the previous one ,use robot lib to press on the difference.

Extra I have read somewhere before that when pressing on a button using robot lib ,it is better to do this robot.mousePress(InputEvent.BUTTON1_MASK); robot.delay(1); robot.mouseRelease(InputEvent.BUTTON1_MASK); instead of this

robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);

Extra thing you can read the game memory from you software and get the color coordinates you want

Share:
14,763
user2220444
Author by

user2220444

Updated on June 04, 2022

Comments

  • user2220444
    user2220444 almost 2 years

    How to find the position of a color that is changing coordinates and needs to be click after being identified.

    Purpose of the program complete tasks in a game, requiring the clicking of different colors which aren't always in the same position.

    Code currently gets color of mouse's coordinates after 5 seconds of executing program


    public class RobotColorClick 
    {
    
        public RobotColorClick () throws AWTException, IOException, InterruptedException 
        {
            Robot robot = new Robot();
    
            //Delay 5 seconds
            robot.delay(5000);        
    
            //Gets color (value of red,green,blue) from the mouse position after 5 seconds 
            Color color = robot.getPixelColor( MouseInfo.getPointerInfo().getLocation().x 
                    , MouseInfo.getPointerInfo().getLocation().y);
    
            //Delay 3 seconds
            robot.delay(3000);
    
            //Mouse moves to X and Y then right click
            //Problem! How to set X and Y to position color coordinates, position will change
            robot.mouseMove(x, y);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
    
        }
    
    
        public static void main(String[] args) throws AWTException, IOException, 
                    InterruptedException 
        {
            new RobotColorClick ();
        }
    }