【问题标题】:Connecting Circles with a Polyline JavaFX用折线 JavaFX 连接圆
【发布时间】:2016-03-03 02:59:38
【问题描述】:

我在 JavaFX 的图表上创建了圆圈,我想用折线连接这些圆圈。有谁知道这样做的语法?谢谢!

【问题讨论】:

    标签: javafx polyline


    【解决方案1】:

    Polyline 可能会起作用,但如果您使用the Path class,您可以更轻松地做到这一点,因为这允许您访问the individual elements of the path (PathElements)。您可以使用绑定将线点的位置绑定到圆的位置。这样,即使您稍后移动圆圈,线条也会保持在适当的位置。

    示例

    private static void bindLinePosTo(Circle circle, LineTo lineTo) {
        lineTo.xProperty().bind(circle.centerXProperty());
        lineTo.yProperty().bind(circle.centerYProperty());
    }
    
    private static void animate(Circle circle, Duration duration, double dy) {
        Timeline animation = new Timeline(
                new KeyFrame(Duration.ZERO, new KeyValue(circle.centerYProperty(), circle.getCenterY())),
                new KeyFrame(duration, new KeyValue(circle.centerYProperty(), circle.getCenterY()+dy)));
        animation.setAutoReverse(true);
        animation.setCycleCount(Animation.INDEFINITE);
        animation.play();
    }
    
    @Override
    public void start(Stage primaryStage) {
        MoveTo start = new MoveTo();
        LineTo line1 = new LineTo();
        LineTo line2 = new LineTo();
    
        Circle c1 = new Circle(10, 100, 5);
        Circle c2 = new Circle(50, 100, 5);
        Circle c3 = new Circle(100, 100, 5);
    
        c1.setFill(Color.RED);
        c2.setFill(Color.RED);
        c3.setFill(Color.RED);
    
        start.xProperty().bind(c1.centerXProperty());
        start.yProperty().bind(c1.centerYProperty());
        bindLinePosTo(c2, line1);
        bindLinePosTo(c3, line2);
    
        Path path = new Path(start, line1, line2);
    
        Pane root = new Pane(path, c1, c2, c3);
    
        animate(c1, Duration.seconds(1), 100);
        animate(c2, Duration.seconds(2), 50);
        animate(c3, Duration.seconds(0.5), 150);
    
        Scene scene = new Scene(root, 110, 300);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    

    【讨论】:

      【解决方案2】:

      使用Binding API,例如。 G。 like this.

      【讨论】:

        猜你喜欢
        • 2014-09-13
        • 1970-01-01
        • 1970-01-01
        • 2018-05-02
        • 1970-01-01
        • 1970-01-01
        • 2016-04-18
        • 1970-01-01
        • 2019-02-19
        相关资源
        最近更新 更多