Search code examples
javaawtrobot

Send cursor to square of pixels


I'm trying to find a way to send the cursor to a square of pixels on the screen. Here, I've got some code that can send it to a specific position:

package JavaObjects;
import java.awt.AWTException;
import java.awt.Robot;

public class MCur {
    public static void main(String args[]) {
        try {
            // The cursor goes to these coordinates
            int xCoord = 500;
            int yCoord = 500;

            // This moves the cursor
            Robot robot = new Robot();
            robot.mouseMove(xCoord, yCoord);
        } catch (AWTException e) {}
    }
}

Is there perhaps some way that, using similar code, I can establish a range rather than a specific point, such that the cursor goes to some random part of the established square?


Solution

  • Since you're working with as you say a "Square", you may want to use the java.awt.Rectangle class, if you're clicking buttons this is particularly useful as you can define the button boundary instead of a point.

    As for the random radius, this is easily accomplished with java.util.Random

    import java.awt.AWTException;
    import java.awt.Dimension;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.Toolkit;
    import java.util.Random;
    
    public class MoveMouse {
    
        private static final Robot ROBOT;
        private static final Random RNG;
    
        public static void main(String[] args) {
            // grab the screen size
            Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
            // Equivalent to 'new Rectangle(0, 0, screen.width, screen.height)' 
            Rectangle boundary  = new Rectangle(screen);
            // move anywhere on screen
            moveMouse(boundary);
        }
    
        public static void moveMouse(int x, int y, int radiusX, int radiusY) {
            Rectangle boundary = new Rectangle();
            // this will be our center
            boundary.setLocation(x, y);
            // grow the boundary from the center
            boundary.grow(radiusX, radiusY);
            moveMouse(boundary);
        }
    
        public static void moveMouse(Rectangle boundary) {
            // add 1 to the width/height, nextInt returns an exclusive random number (0 to (argument - 1))
            int x = boundary.x + RNG.nextInt(boundary.width + 1);
            int y = boundary.y + RNG.nextInt(boundary.height + 1);
            ROBOT.mouseMove(x, y);
        }
    
        // initialize the robot/random instance once when the class is loaded
        // and throw an exception in the unlikely scenario when it can't 
        static {
            try {
                ROBOT = new Robot();
                RNG = new Random();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
    }
    

    This is a basic demonstration.

    You may need to add negative/out-of-range value checks and so on so that it doesn't try to click off the screen.