I'm connecting two nodes by a line. I can drag a line from the circle from one node to the circle of another by drag and drop a CubicCurve.
My Nodes look like this:
My problem is, that after I drop my CubicCurve and it sets the start and end points, the 'Anchor' points are height/2 and width/2 of my DragNode. But I want them to be in the center of my circles (left or right of my Nodes).
My current bindEnds()-Function, where I link the Curves to the Center of my DragNode (AnchorPane):
public void bindEnds (DragNode source, DragNode target) {
cubicCurve.startXProperty().bind(
Bindings.add(source.layoutXProperty(), (source.getWidth() / 2.0)));
cubicCurve.startYProperty().bind(
Bindings.add(source.layoutYProperty(), (source.getWidth() / 2.0)));
cubicCurve.endXProperty().bind(
Bindings.add(target.layoutXProperty(), (target.getWidth() / 2.0)));
cubicCurve.endYProperty().bind(
Bindings.add(target.layoutYProperty(), (target.getWidth() / 2.0)));
source.registerLink (getId());
target.registerLink (getId());
}
I'm thinking about to change my bindEnds()-Function to something like this, where I have my nodes and also their child circles and their centers, where I want to bind my linking curves:
public void bindEnds (DragNode source, DragNode target, Circle c1, Circle c2) {
source.getChildren().add(c1);
target.getChildren().add(c2);
cubicCurve.startXProperty().bind(
Bindings.add(source.layoutXProperty(), (c1.getLayoutX())));
cubicCurve.startYProperty().bind(
Bindings.add(source.layoutYProperty(), (c1.getLayoutY())));
cubicCurve.endXProperty().bind(
Bindings.add(target.layoutXProperty(), (c2.getLayoutX())));
cubicCurve.endYProperty().bind(
Bindings.add(target.layoutYProperty(), (c2.getLayoutY())));
source.registerLink (getId());
target.registerLink (getId());
}
and in my Window ControllerClass:
private void buildDragHandlers() {
this.setOnDragDone (new EventHandler <DragEvent> (){
@Override
public void handle (DragEvent event) {
DragContainer container = (DragContainer) event.getDragboard().getContent(DragContainer.AddNode);
container = (DragContainer) event.getDragboard().getContent(DragContainer.AddLink);
if (container != null) {
String sourceId = container.getValue("source");
String targetId = container.getValue("target");
if (sourceId != null && targetId != null) {
NodeLink link = new NodeLink();
rightAnchor.getChildren().add(0,link);
DragNode source = null;
DragNode target = null;
for (Node n: rightAnchor.getChildren()) {
if (n.getId() == null)
continue;
if (n.getId().equals(sourceId)){
source = (DragNode) n;
}
if (n.getId().equals(targetId)){
target = (DragNode) n;
}
}
if (source != null && target != null){
source.link(target);
link.bindEnds(source, target, c1, c2);
}
}
}
}
});
In my DragNode controller class:
private ArrayList<Circle> circles = new ArrayList<Circle>();
private Circle getNearestCircle(DragNode source) {
Circle nearestCircle = null;
for (Circle circle : circles) {
if (nearestCircle == null) {
nearestCircle = circle;
} else {
}
}
return nearestCircle;
}
public void link (DragNode source) {
getNearestCircle(source).centerXProperty().bindBidirectional(source.getNearestCircle(this).centerXProperty());
getNearestCircle(source).centerYProperty().bindBidirectional(source.getNearestCircle(this).centerYProperty());
}
I have to make it accessible to the Circles I use and also put them inside link.indEnds(source, target);
Can anyone help me?