将连接 link 设置为子圆的中心而不是节点

Set connected link to center of child circle instead of Node

我用一条线连接两个节点。我可以通过拖放 CubicCurve 将一条线从一个节点的圆圈拖到另一个节点的圆圈。

我的节点是这样的:

我的问题是,在我放下 CubicCurve 并设置起点和终点后,'Anchor' 点是我的 DragNode 的 height/2 和 width/2。但我希望他们在我的圈子的中心(我的节点的左侧或右侧)。

我当前的 bindEnds()-函数,其中 link 到我的 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());       
}

我正在考虑将我的 bindEnds()-Function 更改为这样的东西,其中我有我的节点以及它们的子圆和它们的中心,我想在其中绑定我的 linking 曲线:

       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());
} 

在我的 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);                          
                      }
                  }

            }
        }
    });

在我的 DragNode 控制器中 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());
}

我必须让我使用的圈子可以访问它,并将它们放入 link.indEnds(source, target);

谁能帮帮我?

让您的圈子 class 成为您的 DragNode 成员。然后你可以创建一个方法,例如target.getNearestCircle() 获取最接近拖动节点的圆。下面的代码或多或少是元代码,但我希望你能理解:

拖动节点:

public class DragNode {
    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 {
                // If this circle is closer to the target than the current nearest circle, set this circle as the nearestCircle.
            }
        }
        return nearestCircle;
    }

    public void link (DragNode source) {
        // Bind the x and y property of the target and source circle closest to each other.  
        getNearestCircle(source).xProperty.bindBidirectional(source.getNearestCircle(this).xProperty())
        getNearestCircle(source).yProperty.bindBidirectional(source.getNearestCircle(this).yProperty())
    }
}