I am trying to make a little game and in the most games the mouse gets locked in the center of the screen. So, is it possible to lock the mouse in the center of the screen or set the position of the mouse in JavaFX? I know that it is possible to do, and I also know some samples written in LWJGL or just with the AWT/SWING package.
Thanks for help.
-
1)In Swing you can do this using Robot,an example here:programmersheaven.com/discussion/306819/… 2)Maybe making the mouse transparent is a solution for you?GOXR3PLUS– GOXR3PLUS2016年05月29日 06:39:24 +00:00Commented May 29, 2016 at 6:39
-
I'm looking for exactly the same, please share a solution if you've found oneYannick Gottschalk– Yannick Gottschalk2016年05月29日 11:31:45 +00:00Commented May 29, 2016 at 11:31
-
@GoXr3Plus Thank you for responding, but sadly the MouseTransparency is not what I'm looking for.MrMechanik– MrMechanik2016年05月29日 14:31:05 +00:00Commented May 29, 2016 at 14:31
-
You want to move the mouse programmatically in javaFX?That's the whole point?GOXR3PLUS– GOXR3PLUS2016年05月29日 20:27:01 +00:00Commented May 29, 2016 at 20:27
-
@GoXr3Plus well, yes I'm trying to, as said, "set" the position of the Mouse while the transpareny only causes to disable the MouseEvents on the Node.MrMechanik– MrMechanik2016年05月30日 20:14:39 +00:00Commented May 30, 2016 at 20:14
1 Answer 1
Update 11/27/2019
From now you can use also JavaFX Robot API: https://openjfx.io/javadoc/11/javafx.graphics/javafx/scene/robot/Robot.html
Here is the code you need:
import java.awt.AWTException;
import java.awt.Robot;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class MoveCursor extends Application {
Scene scene;
VBox container;
Button moveMouse;
Button showHideCursor;
public static int screenWidth = (int) Screen.getPrimary().getBounds().getWidth();
public static int screenHeight = (int) Screen.getPrimary().getBounds().getHeight();
@Override
public void start(Stage stage) throws Exception {
// MoveMouse Button
moveMouse = new Button("Move Cursor to the center of Screen");
moveMouse.setOnAction(m -> {
moveCursor(screenWidth/2, screenHeight/2);
});
// ShowHide Cursor
showHideCursor = new Button("Show/Hide Cursor");
showHideCursor.setCursor(Cursor.HAND);
showHideCursor.setOnAction(m -> {
if (scene.getCursor() != Cursor.NONE)
scene.setCursor(Cursor.NONE);
else
scene.setCursor(Cursor.DEFAULT);
});
// Container
container = new VBox();
container.getChildren().addAll(moveMouse, showHideCursor);
// Scene
scene = new Scene(container, 500, 500);
stage.setScene(scene);
stage.show();
}
/**
* Move the mouse to the specific screen position
*
* @param x
* @param y
*/
public void moveCursor(int screenX, int screenY) {
Platform.runLater(() -> {
try {
Robot robot = new Robot();
robot.mouseMove(screenX, screenY);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
}
public static void main(String[] args) {
launch(args);
}
}
answered May 31, 2016 at 0:38
GOXR3PLUS
7,31510 gold badges52 silver badges105 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
MrMechanik
awesome! I didn't knew that you can use AWT in FX. Thank you so much!
GOXR3PLUS
JavaFX is missing(for now) some basic things for example(Robot and ImageIO) that AWT has.Generally try to avoid mixing these two cause they are running in different threads and may cause problems...Thats why i am using Platform.runLater in the example()..
GOXR3PLUS
@Slaw You can also use openjfx.io/javadoc/11/javafx.graphics/javafx/scene/robot/…
lang-java