I want to make an app that measures the cursor\'s distance from the center of a component and then moves the cursor back to the center (like most PC video games do). Does an
Robot class can do the trick for you. Here is a sample code for moving the mouse cursor:
try {
// These coordinates are screen coordinates
int xCoord = 500;
int yCoord = 500;
// Move the cursor
Robot robot = new Robot();
robot.mouseMove(xCoord, yCoord);
} catch (AWTException e) {
}
Hi this will just be adding on. I use a Raspberry PI a lot so I've had to learn how to optimize my code this will be a lot shorter.
try {
//moves mouse to the middle of the screen
new Robot().mouseMove((int) Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2, (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2);
//remember to use try-catch block (always, and remember to delete this)
} catch (AWTException e) {
e.printStackTrace();
}
don't forget to import:
import java.awt.*;