Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Drawing a line between two nodes

978168Dec 6 2012 — edited Dec 6 2012
I am busying writing a simple application using JavaFX2. The goal is just to plot 2 nodes (the nodes are movable by dragging them) and then have a function to draw lines between these nodes. I finished the functions to add and move nodes (at the moment I am just using Ellipse shapes but I am going to replace it later with my own node class) but now I am struggling with the connecting lines. The actions to add a node or a line is from a dropdown menu and I have the following code on the line function:
 private void drawLine(MenuItem line) {

    final BooleanProperty lineActive = new SimpleBooleanProperty(false);
    final BooleanProperty clickOne = new SimpleBooleanProperty(false);
    final BooleanProperty clickTwo = new SimpleBooleanProperty(false);

    line.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent t) {              
            lineActive.set(true);
        }
    });

    nodeGroup.setOnMousePressed(new EventHandler<MouseEvent>() {
        public void handle(final MouseEvent t1) {
            clickOne.set(true);

            if (lineActive.get()) {

                if (clickOne.get()) {
                    //get x and y of first node
                    x1 = ((Ellipse) t1.getTarget()).getCenterX();
                    y1 = ((Ellipse) t1.getTarget()).getCenterY();
                    clickOne.set(false);
                    clickTwo.set(true);
                }

                if (clickTwo.get()) {
                    nodeGroup.setOnMouseClicked(new EventHandler<MouseEvent>() {
                        public void handle(MouseEvent t2) {
                            //get x and y of second node
                            x2 = ((Ellipse) t2.getTarget()).getCenterX();
                            y2 = ((Ellipse) t2.getTarget()).getCenterY();

                            //draw line between nodes
                            final Line line = new Line();
                            line.setStartX(x1);
                            line.setStartY(y1);
                            line.setEndX(x2);
                            line.setEndY(y2);

                            canvas.getChildren().add(line);

                            clickTwo.set(false);
                            lineActive.set(false);                     
                        }
                    });
                }
            }
        }
    });
}
I just have the booleans to check for the first and second click to get the center of each node. My first question is when I click on the line function and add a line between 2 nodes, it doesn't seem to end the function, and any other nodes I click on gets a line to it. How can I prevent it from executing more than once.

And my second question is how can I "connect" the line to the nodes that if the node moves, the line stays in the center of the node?

Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 3 2013
Added on Dec 6 2012
3 comments
1,834 views