ボタンは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);
    }

}

UPDATE:私は= 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();
    }
jewelsea:

あなたが動けなくなる場合には、自分が最初にそれを試してみてください、しかし、あなたはそれがどのように行われるか見ることができるように、私は解決策を投稿します。

  1. 作成ランダムあなたの中のアプリケーションのinitメソッドを
  2. あなたはクリック数を増やす場合は、使用してint型のカップルを取得random.nextInt(bound)ランダムからを。
  3. 円を設定centerXし、centerYあなたのランダムな値に。
  4. 完了。

OK、よく、非常に、あなたがそれをしようとすると、あなたは見つける問題を行っていない、それは動作しないことである(これは、あなたがしなければならない)、円が設定したxとyの値を中央に思えません。サークルは、あなたの手動レイアウトの設定を(ただし値を変換せず)を無視するレイアウトペイン(FlowPane)、であるためです。解決策は、(それはすべてのものの下にあるように、最初の項目として)グループで円を入れた後、グループ内のフローペインを入れて、FlowPaneで、あなたのボタンがあり、カウンターをクリックすることです。

あなたは周りに図形を移動するために使用することができますが、レイアウトのためのアニメーションではないため移動X / Yを確保したいともその理由のために使用することがOKであるかもしれません。

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);
    }
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=335718&siteId=1