箭头循环移动动画 JavaFX方式

64 篇文章 12 订阅
package pathmove;

import javafx.animation.Interpolator;
import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.util.ArrayList;
import java.util.List;

public class PathMoveTest extends Application {
    /**
     * 箭头流速,每秒120
     */
    private final static double speed = 120;

    /**
     * 箭头路径点
     */
    private final static List<Point2D> points = getPoints();

    /**
     * 箭头是否是正方向
     */
    private final static boolean isForward = true;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        Pane pane = new Pane();
        pane.setPrefHeight(400);
        pane.setPrefWidth(800);

        Polyline polyline = new Polyline();
        polyline.setStroke(Color.BLUE);
        polyline.setStrokeWidth(1);
        for (Point2D point2D : points) {
            polyline.getPoints().addAll(new Double[]{point2D.getX(), point2D.getY()});
        }

        pane.getChildren().add(polyline);

        // 获得箭头颜色
        Polygon flowTri = new Polygon();
        flowTri.getPoints().addAll(new Double[]{
                0.0, 3.0,
                0.0, -3.0,
                8.0, 0.0});
        Color color = Color.RED;
        flowTri.setFill(color);
        flowTri.setStroke(color);

        flowTri.setCache(true);

        pane.getChildren().add(flowTri);

        PathTransition pathTransition = new PathTransition();
        pathTransition.setNode(flowTri);
        double dist = distance(points);
        pathTransition.setDuration(Duration.seconds(dist / (speed)));

        List<Point2D> point2DList = new ArrayList<>();
        if(isForward){
            point2DList = points;
        }else {
            for (int i = points.size()-1; i >= 0; i--) {
                point2DList.add(points.get(i));
            }
        }

        pathTransition.setPath(getPath(point2DList));

        pathTransition.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
        pathTransition.setCycleCount(Timeline.INDEFINITE);
        pathTransition.setAutoReverse(false);
        pathTransition.setInterpolator(Interpolator.LINEAR);
        pathTransition.play();

        primaryStage.setScene(new Scene(pane, 800, 400));
        primaryStage.show();
    }

    private Shape getPath(List<Point2D> point2DList) {
        Path flowPath = new Path();
        for (int i = 0; i < point2DList.size(); i++) {
            Point2D point2D = point2DList.get(i);
            if (i == 0) {
                flowPath.getElements().add(new MoveTo(point2D.getX(), point2D.getY()));
            }else{
                flowPath.getElements().add(new LineTo(point2D.getX(), point2D.getY()));
            }
        }

        return flowPath;
    }

    /**
     * 计算一条路径的长度
     *
     * @param points
     * @return double
     */
    private static double distance(List<Point2D> points) {
        double distance = 0.0D;
        for (int i = 1; i < points.size(); i++) {
            Point2D p1 = points.get(i);
            Point2D p2 = points.get(i - 1);
            distance += p1.distance(p2);
        }
        return distance;
    }

    private static List<Point2D> getPoints() {
        List<Point2D> points = new ArrayList<>();
        points.add(new Point2D(50, 50));
        points.add(new Point2D(50, 100));
        points.add(new Point2D(100, 100));
        points.add(new Point2D(50, 200));
        points.add(new Point2D(200, 200));
        return points;
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
JavaFX提供了许多内置的动画类和效果,可以帮助开发者轻松创建各种动态效果。下面是一个简单的JavaFX动画示例: ```java import javafx.animation.TranslateTransition; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; import javafx.util.Duration; public class AnimationDemo extends Application { @Override public void start(Stage primaryStage) throws Exception { StackPane root = new StackPane(); Rectangle rect = new Rectangle(50, 50, Color.RED); root.getChildren().add(rect); TranslateTransition translate = new TranslateTransition(Duration.seconds(2), rect); translate.setToX(200); translate.setAutoReverse(true); translate.setCycleCount(TranslateTransition.INDEFINITE); translate.play(); Scene scene = new Scene(root, 300, 300); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 这个示例创建了一个红色的正方形,并将其沿着x轴移动。具体来说,它使用TranslateTransition类来创建动画效果,该类将目标节点从一个位置移动到另一个位置。在这个示例中,我们将正方形沿着x轴移动了200个单位,并使用setAutoReverse(true)设置了自动反向运动,以便它来回移动。我们还使用setCycleCount(TranslateTransition.INDEFINITE)将动画设置为无限循环。最后,我们调用了play()方法来启动动画JavaFX还提供了其他的动画类,例如: - FadeTransition:用于淡入淡出效果 - ScaleTransition:用于缩放效果 - RotateTransition:用于旋转效果 - PathTransition:用于沿着路径移动的效果 这些类的使用方式与TranslateTransition类类似,可以根据需求选择适合的动画类来创建各种动态效果。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风铃峰顶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值