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

时间:2016-11-17 12:59:47

标签: java javafx javafx-8 nodes scenebuilder

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

我的节点看起来像这样:

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() - 函数改为这样的东西,我有节点,还有他们的子圈和它们的中心,我想绑定我的链接曲线:

       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控制器类中:

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

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

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

DragNode:

class Foo {
    constructor () {
        this.name = 'foo';
    }
}
function Bar () {
    this.name = 'bar';
}

function doSmth (anyArg) {
    if (typeof anyArg === 'function') {
        var obj = { someProp: 'qux' };
        try {
          anyArg.call(obj);
        } catch(e) {
          var x = Reflect.construct(anyArg, []);
          Object.assign(obj, x);
        }
        return obj;
    }
}


doSmth(Bar);
doSmth(Foo);