1

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?

  • Searching your question online I found this which may be helpful: (assuming you mean mouse cursor by cursor): https://stackoverflow.com/questions/2941324/how-do-i-set-the-position-of-the-mouse-in-java – LuckyBandit74 Sep 15 '22 at 14:11

1 Answers1

0

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);
}