Why is the window jerks while dragging?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
17 views
The problem is that the window shakes when you drag and it deviates from the cursor. Why can it be?
Here is the code:
package youpackagename; import java.awt.Color; import javafx.application.Application; import javafx.event.EventHandler; import javafx.fxml.FXMLLoader; import javafx.geometry.Rectangle2D; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.input.MouseEvent; import javafx.stage.Screen; import javafx.stage.Stage; import javafx.stage.StageStyle; /** * * @author keeptoo */ public class JavaFXMovable extends Application { //define your offsets here private double xOffset = 0; private double yOffset = 0; @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); //you can use underdecorated or transparent. stage.initStyle(StageStyle.TRANSPARENT); //the stage.initStyle(StageStyle.UNDERDECORATED); //grab your root here root.setOnMousePressed(new EventHandler() { @Override public void handle(MouseEvent event) { xOffset = event.getSceneX(); yOffset = event.getSceneY(); } }); //move around here root.setOnMouseDragged(new EventHandler() { @Override public void handle(MouseEvent event) { stage.setX(event.getScreenX() - xOffset); stage.setY(event.getScreenY() - yOffset); } }); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } }
by | 17 views

1 Answer

0 like 0 dislike
Duplicate response from the comments.

Try this: in the first handler:
xOffset = stage.getX() - event.getScreenX(); yOffset = stage.getY() - event.getScreenY();


And setOnMouseDragged:
stage.setX(event.getScreenX() + xOffset); stage.setY(event.getScreenY() + yOffset);
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked May 21, 2019 by krembrule2016
110,608 questions
257,186 answers
0 comments
35,406 users