首页 > 解决方案 > 动画在两个方向而不是仅向右跟踪 JavaFX 行

问题描述

我编写了一些代码,在 TextArea 的插入符号的右上角生成一行,并在我左键单击时创建以下动画:

我不能为此使用实际的圆形或任何其他形状,因为圆形边缘在扩展时也会变形。

这条线也必须是 BorderPane 的子项,项目的其他组件也是如此。我也真的不想为此使用 JTextArea,因为我从未使用过 swing。

我将只包含与这一行相关的代码部分,因为整个项目相当长且混乱,但我可以向您保证 Eclipse 给出的错误或警告为零。宽度增加完美,但线在 X 轴上的两个方向上扩展,而不是仅修改 endX 坐标。

我还应该提到,我已经尝试在一个单独的项目中测试线条扩展动画(如果有帮助,在窗格中的空白画布上)并且它完全按预期工作。

这是不起作用的项目:

//coordinates of top-left corner of textarea:
        //X: 30
        //Y: 184


        BorderPane pane = new BorderPane();

        EventHandler<MouseEvent> eventHandler = new EventHandler<MouseEvent>() {
               @Override
               public void handle(MouseEvent e) {
                  TextAreaSkin skin = (TextAreaSkin) textarea.getSkin();
                  Bounds b = skin.getCaretBounds();
                  //coordinates of the upper tip of the caret, (0,0) being the top-left corner of textarea:
                  //b.getCenterX();
                  //b.getMinY();
                  int adjust = 7;
                  //type a space char (to which l should be tied, haven't written this part yet)
                  int spacep = textarea.getCaretPosition();
                  textarea.setText(textarea.getText().substring(0, spacep) + " " + textarea.getText().substring(spacep, textarea.getText().length()));

                  //growing animation
                  int sx = (int)(30 + b.getMaxX() + adjust);
                  int sy = (int)(184 + b.getMinY() + adjust-1);
                  int ex = (int)(30 + b.getMaxX() + adjust);
                  int ey = (int)(184 + b.getMinY() + adjust-1);
                  Line l = new Line(sx, sy, ex, ey);
                  l.setStroke(Color.LIGHTGRAY);
                  l.setStrokeLineCap(StrokeLineCap.ROUND);
                  l.setStrokeWidth(1);
                  pane.getChildren().add(l);
                  ScaleTransition s = new ScaleTransition(Duration.millis(500), l);
                  s.setByX(16);
                  s.setByY(16);
                  s.setCycleCount(1);
                  s.setAutoReverse(false);


                  //elongating animation (not working properly!!!)
                Timeline t = new Timeline(
                        new KeyFrame(
                                Duration.millis(1000),
                                new KeyValue(l.endXProperty(), ex + 70)
                        )
                );
                t.setCycleCount(1);
                t.setAutoReverse(false);

                SequentialTransition sequentialTransition = new SequentialTransition();
                sequentialTransition.getChildren().addAll(
                        s,
                        t);
                sequentialTransition.setCycleCount(1);
                sequentialTransition.setAutoReverse(true);
                sequentialTransition.play();


               }
            };
        textarea.addEventFilter(MouseEvent.MOUSE_CLICKED, eventHandler);

        pane.setTop(lblbox);
        pane.setCenter(textarea);
        pane.setStyle(String.format("-fx-background-color: #%s;", s));
        BorderPane.setMargin(textarea, new Insets(30));
        primaryStage.setScene(new Scene(pane, 500, 500));
        primaryStage.show();
    }

这是测试项目(有效):

public class FXSample extends Application {

    public static void main(String[] args) {
        launch(args);
    }
    final int W = 800;
    final int H = 500;
    @Override
    public void start(Stage primaryStage) throws Exception{
        primaryStage.setTitle("Definition of Color");
        Canvas canvas = new Canvas(W, H);
        GraphicsContext gc = canvas.getGraphicsContext2D();

        Line l = new Line(200, 400, 200, 400);
        l.setStroke(Color.LIGHTGRAY);
        l.setStrokeLineCap(StrokeLineCap.ROUND);
        l.setStrokeWidth(16);
        Timeline t = new Timeline(
                new KeyFrame(
                        Duration.millis(1000), 
                            new KeyValue(l.endXProperty(), 500)
            )
        );
        t.setCycleCount(2);
        t.play();

        Pane root = new Pane();
        root.getChildren().add(l);
        root.getChildren().add(canvas);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}

正如您从第一个片段中看到的那样,我尝试更改很多东西以使其工作,包括将坐标从 double 转换为 int 并为 startXY 和 endXY 创建单独的变量,即使它们具有相同的值开始。

如果您需要更多详细信息,请告诉我!抱歉,顺便说一句令人困惑的措辞,我真的是 java 新手,我的英语不是最好的。

标签: javaeclipsejavafx

解决方案


推荐阅读