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

标签 java javafx javafx-8 nodes scenebuilder

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

我的节点如下所示:

enter image description here

我的问题是,在我放下 CubicCurve 并设置起点和终点后,“ anchor ”是我的 DragNode 的高度/2 和宽度/2。但我希望它们位于我的圆圈中心(节点的左侧或右侧)。

我当前的bindEnds()函数,我将曲线链接到我的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()函数更改为这样的函数,其中我有我的节点以及它们的子圆和它们的中心,我想在其中绑定(bind)我的链接曲线:

       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 Controller 类中:

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

我必须使其可供我使用的 Circles 访问,并将它们放入 link.indEnds(source, target);

有人可以帮助我吗?

最佳答案

只需使您的 Circles 类成为 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())
    }
}

关于java - 将连接的链接设置为子圆的中心而不是节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40655611/

相关文章:

java - Spring 验证修复

java - 将字符串转换为 List<Long>

java - 如何从本地目录加载resources.properties,而不是Java中的JAR?

JavaFX 应用程序在使用多线程时出现滞后

Java 忽略 jar 文件 list 中的类路径条目

java - 区分 Eclipse 装饰器中的包和文件夹

javafx tableview 编辑同一行的另一个单元格后更新单元格

JavaFX 多窗口问题

java - Spring Boot 桌面独立应用程序中的 Apache Shiro

JavaFX 11 : Add a graphic to a TitledPane on the right side