Is this the best I can get to making a node draggable?

basically I've been trying to figure out how to properly make nodes draggable. Usually I always just add an offset so that the node appears to be dragged by where the mouse clicked but it's just the center of the node. I wanted to try and see If I could do it but that code that I used hasn't been that reliable either. Is there a better approach here?
7 Replies
JavaBot
JavaBot3y ago
Hey, @Kar.Jar! Please remember to /close this post once your question has been answered!
Carter
CarterOP3y ago
public class Draggable {

private double xPosition;
private double yPosition;

public void makeDraggable(Node n) {

n.setOnMousePressed(event -> {
xPosition = event.getSceneX();
yPosition = event.getSceneY();

});

n.setOnMouseDragged(event -> {

n.setLayoutX(event.getSceneX() - xPosition);
n.setLayoutY(event.getSceneY() - yPosition);

});

}


}
public class Draggable {

private double xPosition;
private double yPosition;

public void makeDraggable(Node n) {

n.setOnMousePressed(event -> {
xPosition = event.getSceneX();
yPosition = event.getSceneY();

});

n.setOnMouseDragged(event -> {

n.setLayoutX(event.getSceneX() - xPosition);
n.setLayoutY(event.getSceneY() - yPosition);

});

}


}
here is the code that sets a not to be draggable and here is the result
Carter
CarterOP3y ago
as you can see the motion kind of jerks around
Carter
CarterOP3y ago
Overall it's not too bad I just want to know if there is an easy fix for this or not.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Carter
CarterOP3y ago
yeah it should be readable enough I'm pretty much doing the same thing
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server