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;
}
}
箭头循环移动动画 JavaFX方式
最新推荐文章于 2024-06-17 10:41:41 发布