在 JavaFX 中按下按钮时如何创建随机圆圈位置?

How to create random circle locations when Button is pressed in JavaFX?

我正在尝试从 Udemy 课程中学习 Java,我被要求在进入以下部分之前创建一个作业。

作业是编写一个 JavaFX 应用程序,显示一个圆圈和一个按钮,每次按下所述按钮时,圆圈应移动到随机位置。

到目前为止我已经创建了一个代码来计算我点击按钮的次数,现在我想随着计数移动圆圈:

public class CircleJumper extends Application {

    // Declare Variables
    private int count;
    private Circle initCircle;
    private Button initButton;
    private Text countText;

    /*
    *
    * Write function here
    *
    */
    @Override
    public void start(Stage primaryStage){

        // Initiate Variables
        count = 0;
        initCircle = new Circle(30, -50, 30);
        initButton = new Button("Click Me!");
        countText = new Text("Clicks: 0");
        // Here its where I built the click counter
        initButton.setOnAction((event) -> {
            count++;
            countText.setText("Pushes: " + count);
        });        
        ;
        Group baseDemo = new Group(initButton, countText);

        FlowPane pane = new FlowPane(baseDemo, initCircle);
        pane.setAlignment(Pos.CENTER);
        pane.setHgap(20);
        pane.setStyle("-fx-background-color: cyan");

        Scene scene = new Scene(pane, 600, 300);

        primaryStage.setTitle("Draw a Circle when Button is Pressed");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        launch(args);
    }

}

更新: 我决定将我的变量计数声明为 = Math.random();并创建了一个圆圈。之后,点击按钮应该得到生成的 Math.random 并将其放入 setTranslate 中,对吗?

    public void start(Stage primaryStage){

        // Initiate Variables
        count = Math.random();
        initCircle = new Circle(30, 50, 30);
        initButton = new Button("Click Me!");

        Group circlePos = new Group(initCircle);
        circlePos.setTranslateX(10);
        circlePos.setTranslateY(10);

        initButton.setOnAction((event) -> {
            count++;
            circlePos.setTranslateY(count);
            circlePos.setTranslateX(count);
        });

        Group baseDemo = new Group(initButton);

        FlowPane pane = new FlowPane(baseDemo, initCircle);
        pane.setAlignment(Pos.CENTER);
        pane.setHgap(20);
        pane.setStyle("-fx-background-color: cyan");

        Scene scene = new Scene(pane, 600, 300);

        primaryStage.setTitle("Draw a Circle when Button is Pressed");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

先自己尝试一下,但我会 post 一个解决方案,以便您了解它是如何完成的,以防您遇到困难。

  1. 创建 random in your application init method
  2. 在您增加点击次数的地方,get a couple of ints using random.nextInt(bound) 来自随机。
  3. 将圆 centerXcenterY 设置为您的随机值。
  4. 完成。

好的,还没有完全完成,当你尝试(你应该)时会发现的问题是它不起作用,圆似乎不会以你设置的 x 和 y 值为中心.这是因为 Circle 位于布局窗格 (FlowPane) 中,它将忽略您的手动布局设置(尽管不会转换值)。解决方案是将圆圈放在组中(作为第一项,以便它在所有内容下),然后将流程窗格放在组中,并且在 FlowPane 中你有你的按钮和点击计数器。

您可能希望为动画而不是布局保留 translateX/Y,尽管它可以用来移动形状,因此也可以使用。

import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.util.Random;

public class CircleJumper extends Application {
    private static final double MAX_X = 600;
    private static final double MAX_Y = 300;

    private int clickCount = 0;
    private Random random;

    @Override
    public void init() {
        random = new Random();
    }

    @Override
    public void start(Stage stage){
        Circle circle = new Circle(MAX_X / 2, MAX_Y / 2, 30);
        Button button = new Button("Click Me!");
        Text clickCountText = new Text("Clicks: " + clickCount);

        button.setOnAction((event) -> {
            clickCount++;
            clickCountText.setText("Clicks: " + clickCount);

            circle.setCenterX(random.nextInt((int) MAX_X));
            circle.setCenterY(random.nextInt((int) MAX_Y));
        });        

        Group layout = new Group(
                circle,
                new FlowPane(button, clickCountText)
        );

        stage.setScene(new Scene(layout, MAX_X, MAX_Y, Color.CYAN));
        stage.setResizable(false);
        stage.show();
    }

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