【问题标题】:Generate 10 random circles and points them in the path生成 10 个随机圆圈并将它们指向路径
【发布时间】:2019-07-27 08:05:09
【问题描述】:

我想要什么:我需要随机生成 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();

【问题讨论】:

  • “我不能用随机坐标创建 10 个随机圆圈”到底是什么阻止了你?您是否面临任何特定问题?它是否在某个范围内生成数字?还是重复这个过程?或者是其他东西?考虑添加伪解决方案、您认为您的应用程序应该具有的步骤,并描述您遇到问题的步骤。
  • 不,我想用 for 循环来做到这一点,但我只是不知道如何,我可以像 Circle circle1 = new Circle(); 那样创建它们 ... 10 次。是的,我给出了错误的解释..我想用 for 循环创建 10 个圆圈,并用随机坐标将它们放在不重叠的位置,然后我想将它们指向路径,比如用线连接它们,然后在那条线上我将添加一个正方形。
  • 考虑创建像MyCircle 这样的类,它将存储x,y,r。然后向它添加方法,该方法将检查它是否与boolean overlaps(MyCycle otherCycle){ return distanceFromCenters(this, otherCycle)<this.r+otherCycle.r;} 等其他循环重叠。现在只需循环直到你有 10 个圆圈,在每次迭代中创建一个圆圈,将其添加到某个列表中,但只有在确认它不与已存储在列表中的其他圆圈重叠之后。如果它甚至与一个重叠,则生成另一个圆圈,直到您可以将其添加到列表中。重复 10 次。
  • 看起来您是第一次使用,请考虑以下提示:在提出问题时,请尝试详细说明您为该问题付出的努力。一旦人们注意到你的努力,他们就会尝试回应。也尽量让你的问题尽可能狭窄和具体。大多数用户不会对广泛的问题表现出兴趣。在你的问题中,我们看不到你的任何努力。甚至您粘贴的代码也几乎是文档中的代码。尝试创建Minimal, Complete, and Verifiable example 并指出您面临的确切问题。

标签: java animation javafx


【解决方案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);
    }
}

【讨论】:

    猜你喜欢
    • 2018-02-15
    • 2018-03-25
    • 1970-01-01
    • 2015-07-30
    • 2017-01-04
    • 2018-09-19
    • 2022-08-14
    • 1970-01-01
    相关资源
    最近更新 更多