java - JavaFX: Having issues making a draggable node -
i'm attempting make node i've created draggable.
i'm able place nodes @ point in pane, when comes dragging them around, seem automatically offput.
i've tried making delta class contain x , y updated accordingly during pressing of mouse on node.
nfapane.setonmousepressed(event -> { if (!event.getbutton().equals(mousebutton.primary)) return; (final nfanode node : nfanodes) if (node.ishover()) return; final nfanode nfanode = new nfanode(nfanodes.size(), event.getx(), event.gety()); nfapane.getchildren().add(nfanode); nfanodes.add(nfanode); delta dragdelta = new delta(); nfanode.setonmousepressed(event1 -> { dragdelta.setdragdeltax(nfanode.getlayoutx()); dragdelta.setdragdeltay(nfanode.getlayouty()); }); nfanode.setonmousedragged(event1 -> { nfanode.moveto(event1.getscenex() + dragdelta.getdragdeltax(), event1.getsceney() + dragdelta.getdragdeltay()); }); });
the node defined class nfanode:
package sample; import javafx.scene.group; import javafx.scene.paint.color; import javafx.scene.shape.ellipse; import javafx.scene.text.font; import javafx.scene.text.text; /** * created david on 11/5/2016. */ public class nfanode extends group { //private list<nfanode> next; public static final font nfafont = font.font("footlight mt light", 25); private final ellipse bubble; private final text text; private double textwidth; public nfanode(int value, double x, double y) { this.text = new text(x, y + 5, ""); renumber(value); this.text.setfont(nfafont); textwidth = this.text.getboundsinlocal().getwidth(); bubble = new ellipse(x, y, this.text.getboundsinlocal().getwidth() + 5, 25); this.text.setx(x - textwidth / 2); bubble.setstroke(color.black); bubble.setfill(color.white); getchildren().addall(bubble, this.text); } public text gettext() { return text; } public void renumber(final int value) { final stringbuilder builder = new stringbuilder(); (char c : string.valueof(value).tochararray()) builder.append((char) (c + 8272)); text.settext("q" + builder.tostring()); } public void moveto(final double x, final double y) { bubble.setcenterx(x); bubble.setcentery(y); text.setx(x - textwidth / 2); text.sety(y + 5); } }
i'm confused why affect position of node when dragged?
you forgot take mouse position account when it's pressed.
you've got following equation
mouseposition - mousepressedposition = targetposition - startposition
so dragdelta
should be
startposition - mousepressedposition = targetposition - mouseposition
personally prefer using coordinate system of parent, using scene coordinates should work too, unless you've got transformations in place, not translations.
example
private static class delta { private double dragdeltax; private double dragdeltay; public double getdragdeltax() { return dragdeltax; } public double getdragdeltay() { return dragdeltay; } public void setdragdelta(double x, double y) { this.dragdeltax = x; this.dragdeltay = y; } } @override public void start(stage primarystage) { circle c = new circle(30); c.setlayoutx(50); c.setlayouty(50); pane root = new pane(c); delta dragdelta = new delta(); c.setonmousepressed(event1 -> { point2d mouseinparent = c.localtoparent(event1.getx(), event1.gety()); dragdelta.setdragdelta(c.getlayoutx() - mouseinparent.getx(), c.getlayouty()-mouseinparent.gety()); event1.consume(); }); c.setonmousedragged(event1 -> { point2d mouseinparent = c.localtoparent(event1.getx(), event1.gety()); c.setlayoutx(mouseinparent.getx() + dragdelta.getdragdeltax()); c.setlayouty(mouseinparent.gety() + dragdelta.getdragdeltay()); }); scene scene = new scene(root, 500, 500); primarystage.setscene(scene); primarystage.show(); }
Comments
Post a Comment