【问题标题】:JavaFX: animation which uses PathTransition as a drawing penJavaFX:使用 PathTransition 作为绘图笔的动画
【发布时间】:2012-12-30 03:27:56
【问题描述】:

示例代码

//node 
Rectangle rect = new Rectangle (0, 0, 20, 20);

//path
Text text = TextBuilder.create()
                       .text("J a v a F X   R o c k s")
                        .font(new Font(50))
                        .x(65)
                        .y(100)
                        .build();
// path transition
 pathTransition = PathTransitionBuilder.create()
                .duration(Duration.seconds(15))
                .path(text)
                .node(rect)
                .orientation(OrientationType.ORTHOGONAL_TO_TANGENT)
                .cycleCount(Timeline.INDEFINITE)
                .autoReverse(true)
                .build();

我想显示 rect 节点经过的部分文本(路径)。上图中的意思是矩形节点一直到java,我想只在那个时间点显示那个部分..

【问题讨论】:

    标签: animation javafx-2 javafx


    【解决方案1】:

    您可以尝试为 Text 分配一个剪切区域并在动画期间对其进行更新:

    public void start(Stage primaryStage) {
        final Rectangle pen = new Rectangle(0, 0, 20, 20);
    
        // this pane this contain clipping
        final Pane clip = new Pane();
    
        // listener to update clipping area
        ChangeListener changeListener = new ChangeListener() {
            @Override
            public void changed(ObservableValue ov, Object t, Object t1) {
                Rectangle newrect = new Rectangle(pen.getTranslateX(), pen.getTranslateY(), pen.getWidth(), pen.getHeight());
                newrect.setRotate(pen.getRotate());
                clip.getChildren().add(newrect);
            }
        };
    
        // rect coordinates will be changed during animation, so we will listen to them
        pen.translateXProperty().addListener(changeListener);
        pen.translateYProperty().addListener(changeListener);
        pen.rotateProperty().addListener(changeListener);
    
        final Text text = TextBuilder.create()
                .text("J a v a F X   R o c k s")
                .font(new Font(50))
                .clip(clip)
                .x(65)
                .y(100)
                .build();
    
        PathTransition pathTransition = PathTransitionBuilder.create()
                .duration(Duration.seconds(15))
                .path(text)
                .node(pen)
                .orientation(OrientationType.ORTHOGONAL_TO_TANGENT)
                .build();
    
        // once we done we don't want to store thousands of rectangles used to clip
        pathTransition.setOnFinished(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                text.setClip(null);
                clip.getChildren().clear();
            }
        });
    
        Pane root = new Pane();
        root.getChildren().addAll(text, pen);
    
        primaryStage.setScene(new Scene(root, 600, 200));
        primaryStage.show();
        pathTransition.play();
    }
    

    Canvas 对象是一种更有效的存储剪切区域的方法,但在画布上绘制带有旋转的矩形需要一些数学知识,所以由您决定。

    【讨论】:

      猜你喜欢
      • 2015-10-05
      • 2015-07-11
      • 1970-01-01
      • 1970-01-01
      • 2015-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多