2017-07-29 2 views
0

最初のコンテナにあるノードを2番目のコンテナの別のノードに接続するにはどうしたらいいですか?別のペインのサブノードに、私は同じコンテナであるが、私が必要としているものではないノードを接続するように管理しました。2つのノードを線で接続するにはどうすればいいですか

enter code here 
public class Main extends Application { 

    static Pane root = new Pane(); 


    @Override 
    public void start(Stage primaryStage) throws Exception{ 

     Circuit c1 = new Circuit(10,10); 
     Circuit c2 = new Circuit(200,10); 
     Circuit c3 = new Circuit(10,200); 
     Circuit c4 = new Circuit(200,200); 

     root.getChildren().addAll(c1, c2, c3, c4); 

     primaryStage.setScene(new Scene(root, 300, 275)); 
     primaryStage.show(); 
    } 


    public static void main(String[] args) { 
     launch(args); 
    } 
} 

回路クラス

enter code here 

import static sample.Main.root; 

public class Circuit extends Pane{ 

Circuit(int LOCATION_X, int LOCATION_Y){ 
    setStyle("-fx-background-color: red"); 
    setPrefSize(150,150); 

    setLayoutX(LOCATION_X); 
    setLayoutY(LOCATION_Y); 

    createCircle cir = new createCircle(); 

    cir.setLayoutX(75); 
    cir.setLayoutY(75); 

    // register handlers 
    cir.setOnDragDetected(startHandler); 
    cir.setOnMouseDragReleased(dragReleaseHandler); 
    cir.setOnMouseDragEntered(dragEnteredHandler); 

    // add info allowing to identify this node as drag source/target 
    cir.setUserData(Boolean.TRUE); 

    getChildren().add(cir); 



    root.setOnMouseReleased(evt -> { 
     // mouse released outside of a target -> remove line 
     root.getChildren().remove(startHandler.line); 
     startHandler.line = null; 
    }); 
    root.setOnMouseDragged(evt -> { 
     if (startHandler.line != null) { 
      Node pickResult = evt.getPickResult().getIntersectedNode(); 
      if (pickResult == null || pickResult.getUserData() != Boolean.TRUE) { 
       // mouse outside of target -> set line end to mouse position 
       startHandler.line.setEndX(evt.getX()); 
       startHandler.line.setEndY(evt.getY()); 
      } 
     } 
    }); 
} 

    class DragStartHandler implements EventHandler<MouseEvent> { 

     public Line line; 

     @Override 
     public void handle(MouseEvent event) { 
      if (line == null) { 
       Node sourceNode = (Node) event.getSource(); 
       line = new Line(); 
       Bounds bounds = sourceNode.getBoundsInParent(); 

       // start line at center of node 
       line.setStartX((bounds.getMinX() + bounds.getMaxX())/2); 
       line.setStartY((bounds.getMinY() + bounds.getMaxY())/2); 
       line.setEndX(line.getStartX()); 
       line.setEndY(line.getStartY()); 
       sourceNode.startFullDrag(); 
       root.getChildren().add(0, line); 
      } 
     } 
    } 

    DragStartHandler startHandler = new DragStartHandler(); 
    EventHandler<MouseDragEvent> dragReleaseHandler = evt -> { 
     if (evt.getGestureSource() == evt.getSource()) { 
      // remove line, if it starts and ends in the same node 
      root.getChildren().remove(startHandler.line); 
     } 
     evt.consume(); 
     startHandler.line = null; 
    }; 
    EventHandler<MouseEvent> dragEnteredHandler = evt -> { 
     if (startHandler.line != null) { 
      // snap line end to node center 
      Node node = (Node) evt.getSource(); 
      Bounds bounds = node.getBoundsInParent(); 
      startHandler.line.setEndX((bounds.getMinX()+bounds.getMaxX())/2); 

      startHandler.line.setEndY((bounds.getMinY()+bounds.getMaxY())/2); 
     } 
     }; 

} 

点ワイヤあなたは座標系混合され引き出され

enter code here 

public class createCircle extends Circle { 

createCircle(){ 

    super(25, Color.BLACK.deriveColor(0, 1, 1, 0.5)); 
} 

} 

enter image description here

+0

最終的に、接続しているものは同じコンテナにあります(たとえば、どちらも確かにシーンのルートにあります)。直接的ではありません。接続しているノードの共通の祖先にその行を追加します。 –

+0

または上部に見えないペインを追加し、そこに線を引く。 –

+0

共通の祖先に行を追加するとどういう意味ですか?私は別のペインのサブノードを接続したい、共通の祖先は、メインルートがシーンに追加されます。場合は上記のような複数の回路があり、私は回路が接続されている1つを検出する必要があります。 –

答えて

1

に接続される場所から。 (私が正しくあなたのコードを読んでいる場合)

Bounds bounds = sourceNode.getBoundsInParent(); 

はあなたの現在のCircuitのインスタンスになるだろうsourceNodeの親の座標系でsourceNodeの境界を与えるだろう。ただし、これらの境界を使用してルートノードに配置されている線の座標を計算しているので、ルートの座標系に座標が必要です。

は、あなたがラインの座標を計算するためにそれを使用することができますので、今boundsInRootは、rootの座標系におけるsourceNodeの境界を表し

Bounds boundsInScene = sourceNode.localToScene(sourceNode.getBoundsInLocal()); 
Bounds boundsInRoot = root.sceneToLocal(boundsInScene); 

ようなものを実行して座標を変換することができます。コード全体で同様の変換が必要になる可能性があります。

+0

はい私はワイヤを見ることができますが、それでも何か問題があります。最後の回路からワイヤを自由にドラッグできますが、接続しませんが、回路1からラインをドラッグすると、回路2は回路3に接続し、回路3は停止する。 –

関連する問題