JavaFx通过拖放连接两个子节点和一行 [英] JavaFx connecting two child nodes with a line by dragging and dropping

查看:247
本文介绍了JavaFx通过拖放连接两个子节点和一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个节点。我想点击一个节点,将光标拖到另一个节点,并在光标释放后用一条线连接两个节点。

I have two nodes. I would like to click on one node, drag my cursor across to another node, and connect the two nodes with a line once the cursor is released.

我已经设置好了所有的EventHandlers - 拖放行都有效,但它没有绑定到第二个节点。

I have set up all the EventHandlers -- dragging and dropping the line works, but it doesn't bind to the second node.

public class LinkHandler {
    static Node hoverNode;
}

此类用于查找光标悬停在哪个节点上。

This class is used to find what Node the cursor is hovering over.

private void setLinkHandlers(Node node) {
    Line line = new Line();
    this.getChildren().add(line);

    // On mouse hover, set LinkHandler.hoverNode
    node.setOnMouseDragEntered( (MouseEvent mouseEvent) -> {
        LinkHandler.setHoverNode(node);
        System.out.println(node);
    });

    // On mouse exit, remove LinkHandler.hoverNode
    node.setOnMouseDragExited( (MouseEvent mouseEvent) -> {
       // PLEASE NOTE: I have tried commenting and uncommenting the line below. Neither works.
       // LinkHandler.setHoverNode(null);
    });

    // On mouse press, set Line startpoint
    node.setOnMousePressed( (MouseEvent mouseEvent) -> {
        // Stop BorderPane from being dragged
        LinkHandler.isConnecting = true;

        // Bind startpoint to node position
        line.startXProperty().bind(node.layoutXProperty());
        line.startYProperty().bind(node.layoutYProperty());
    });

    // On mouse released, either bind Line to LinkHandler.hoverNode, or remove if hoverNode = null
    node.setOnMouseReleased( (MouseEvent mouseEvent) -> {
        // Allow BorderPane to be dragged again
        LinkHandler.isConnecting = false;

        // If there is a node to connect to...
        if(LinkHandler.getHoverNode() != null) {
            // Bind end position to the node's position
            line.endXProperty().bind(LinkHandler.getHoverNode().layoutXProperty());
            line.endYProperty().bind(LinkHandler.getHoverNode().layoutYProperty());
        } else {
            // Otherwise print error
            System.out.println("Not hovering over a node. Cannot create connection.");
        }
    });

    // Temporarily bind Line endpoint to mouse position.
    node.setOnMouseDragged( (MouseEvent mouseEvent) -> {
        line.setEndX(mouseEvent.getX());
        line.setEndY(mouseEvent.getY());
    });
}

这是很多代码,所以我将尝试总结一下:

This is a lot of code, so I'll try to summarise it:


  • 当dragEntered时,将LinkHandler.hoverNode设置为此节点

  • 当dragExit时,设置LinkHandler.hoverNode null

  • 当mousePressed时,将Line起始位置绑定到此节点位置

  • 当mouseDragged时,临时将Line end position设置为mousePosition

  • 当mouseReleased时,将Line end position绑定到LinkerHandler.hoverNode位置,或者删除Line,hoverNode为null。

  • When dragEntered, set LinkHandler.hoverNode to this node
  • When dragExit, set LinkHandler.hoverNode to null
  • When mousePressed, bind Line start position to this node position
  • When mouseDragged, temporarily set Line end position to mousePosition
  • When mouseReleased, bind Line end position to LinkerHandler.hoverNode position, or remove Line is hoverNode is null.

我相信问题出在这个片段中:

I believe the problem lies in this snippet:

    // On mouse hover, set LinkHandler.hoverNode
    node.setOnMouseDragEntered( (MouseEvent mouseEvent) -> {
        LinkHandler.setHoverNode(node);
        System.out.println(node);
    });

    // On mouse exit, remove LinkHandler.hoverNode
    node.setOnMouseDragExited( (MouseEvent mouseEvent) -> {
       // LinkHandler.setHoverNode(null);
    });

我不认为这是正确调用的。此代码适用的节点是BorderPane的子节点,它具有自己的onMouseDrag功能<<这也可能导致问题。

I don't think this is getting called properly. The Nodes that this code applies to are children of a BorderPane, which has it's own onMouseDrag functionality << this might also be causing a problem.

提前致谢,对不起,如果这个问题有点模糊。我试图具体。

Thanks in advance, sorry if this question is a bit vague. I tried to be specific.

推荐答案

请注意,您永远不会初始化完整的拖动,因此事件永远不会传递到其他节点拖动的起始节点。要更改此设置,请为起始节点调用 startFullDrag()

Note that you never initialize a full drag and therefore the events are never delivered to nodes other that the start node of the drag. To change this, call startFullDrag() for the start node.

以下示例演示如何将行捕捉到中心 Circle s:

The following example demonstrates snapping the lines to the center of Circles:

public static Circle createCircle(double x, double y) {
    return new Circle(x, y, 20, Color.BLACK.deriveColor(0, 1, 1, 0.5));
}

@Override
public void start(Stage primaryStage) {

    Node[] circles = new Node[]{
        createCircle(40, 40),
        createCircle(240, 40),
        createCircle(40, 240),
        createCircle(240, 240)
    };
    Pane root = new Pane(circles);

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

    for (Node n : circles) {
        // register handlers
        n.setOnDragDetected(startHandler);
        n.setOnMouseDragReleased(dragReleaseHandler);
        n.setOnMouseDragEntered(dragEnteredHandler);

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

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

    Scene scene = new Scene(root, 280, 280);

    primaryStage.setScene(scene);
    primaryStage.show();
}

这篇关于JavaFx通过拖放连接两个子节点和一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆