生成10个随机圆并将其指向路径

时间:2019-03-05 19:06:57

标签: java animation javafx

我想要什么:我需要生成带有随机坐标的随机10个圆并将它们指向路径。然后创建一个需要使用动画沿该路径移动的正方形。

我的问题是什么:我无法创建具有随机坐标的10个随机圆,但是我创建了动画代码。

这是我的代码,我创建了随机的曲线和正方形。该曲线只是一个例子,因为我不知道如何用随机坐标制作圆并将其指向路径。

Example of my code

final Rectangle rectPath = new Rectangle(0, 0, 40, 40);
    rectPath.setArcHeight(10);
    rectPath.setArcWidth(10);
    rectPath.setFill(Color.ORANGE);

    Path path = new Path();
    path.getElements().add(new MoveTo(20, 20));
    path.getElements().add(new CubicCurveTo(380, 0, 380, 120, 200, 120));
    path.getElements().add(new CubicCurveTo(0, 120, 0, 240, 380, 240));
    path.getElements().add(new CubicCurveTo(420, 350, 420, 440, 10, 450));

    PathTransition pathTransition = new PathTransition();
    pathTransition.setDuration(Duration.millis(4000));
    pathTransition.setPath(path);
    pathTransition.setNode(rectPath);
    pathTransition.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
    pathTransition.setCycleCount(5);
    pathTransition.setAutoReverse(true);
    pathTransition.play();
    Group root = new Group();
    root.getChildren().add(rectPath);
    root.getChildren().add(path);
    Scene scene = new Scene(root, 600, 450);

    primaryStage.setTitle("Path transition demo");
    primaryStage.setScene(scene);
    primaryStage.show();

1 个答案:

答案 0 :(得分:1)

首先检查我对这个问题的评论。这不是很复杂。这都是关于学习概念并将它们组合在一起的。假设您可能是初学者,请根据您的要求找到以下示例。

import javafx.animation.PathTransition;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

public class RandomPathTransistionDemo extends Application {
    PathTransition pathTransition;
    Path path;

    SecureRandom random = new SecureRandom();

    @Override
    public void start(Stage stage) {
        VBox root = new VBox();
        root.setSpacing(10);
        root.setPadding(new Insets(10));
        Scene scene = new Scene(root, 600, 600);
        stage.setScene(scene);
        stage.setTitle("Random Path Transistion");
        stage.show();

        Pane pane = new Pane();
        pane.setPadding(new Insets(10));
        pane.setStyle("-fx-border-width:1px;-fx-border-color:black;-fx-background-color:white;");
        VBox.setVgrow(pane, Priority.ALWAYS);

        Button generateBtn = new Button("Generate circles");
        Button animationBtn = new Button("Start Animation");
        animationBtn.setDisable(true);

        HBox buttons = new HBox(generateBtn, animationBtn);
        buttons.setSpacing(15);
        root.getChildren().addAll(buttons, new Label("Click generate button as many times as you want !!"),pane);

        final Rectangle rectPath = new Rectangle(0, 0, 20, 20);
        rectPath.setArcHeight(10);
        rectPath.setArcWidth(10);
        rectPath.setFill(Color.ORANGE);

        path = new Path();
        path.setStroke(Color.LIGHTGREEN);
        path.setStrokeWidth(2);

        generateBtn.setOnAction(e -> {
            animationBtn.setDisable(false);
            if (pathTransition != null) {
                pathTransition.stop();
            }
            pane.getChildren().clear();
            path.getElements().clear();

            int width = (int) pane.getWidth() - 20;
            int height = (int) pane.getHeight() - 20;
            List<Circle> dots = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                double x = random.nextInt(width); // Get a random value of x within the pane width
                double y = random.nextInt(height);// Get a random value of y within the pane height

                // If required include your logic to see if this point is not within the range of other points.

                // Create a circle with this random point
                Circle dot = new Circle(x, y, 5, Color.RED);
                dots.add(dot);

                // Also inlcude a path element for this random point.
                path.getElements().add(i == 0 ? new MoveTo(x, y) : new LineTo(x, y));
            }

            // Add all nodes in the pane one after another to have a nice visual.
            pane.getChildren().add(path);
            pane.getChildren().addAll(dots);
            pane.getChildren().add(rectPath);

            // Move the rectangle to the start point of the path.
            rectPath.setTranslateX(dots.get(0).getCenterX() - 10); // 10 :: half of rectangle width
            rectPath.setTranslateY(dots.get(0).getCenterY() - 10); // 10 :: half of rectangle height

        });

        animationBtn.setOnAction(e -> {
            pathTransition = new PathTransition();
            pathTransition.setDuration(Duration.millis(4000));
            pathTransition.setPath(path);
            pathTransition.setNode(rectPath);
            pathTransition.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
            pathTransition.setAutoReverse(false);
            pathTransition.play();
        });
    }

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