How to set the cursor location in a JFrame?

Viewed 34

I'm making a little game in java swing, it's a game where the enemy follows the cursor. But if the cursor exists the frame the application returns an error, so instead of that I made the application quit when the cursor leaves the window. But is there actually a way to directly set the cursor position?

1 Answers

It's can be used:

public static void moveMouse(JFrame jFrame, Point inside) throws AWTException {
        final GraphicsDevice device = jFrame.getGraphicsConfiguration().getDevice();
        final Robot r = new Robot(device);
        final Rectangle bounds = jFrame.getBounds();

        r.mouseMove(bounds.x + inside.x, bounds.y + inside.y);
}
Related