Java 如何在窗格上的随机位置放置形状?

Java 如何在窗格上的随机位置放置形状?,java,Java,我正在做一个最终的项目,我正在做一个有一个点的游戏,这个点会在一个窗口中随机移动,当它被点击时,用户的分数会增加。我想知道如何每半秒左右随机移动点。这是我到目前为止所有的代码,谢谢 import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.stage.Popup; import javafx.stage.Stage;

我正在做一个最终的项目,我正在做一个有一个点的游戏,这个点会在一个窗口中随机移动,当它被点击时,用户的分数会增加。我想知道如何每半秒左右随机移动点。这是我到目前为止所有的代码,谢谢

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Popup;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;


public class CatchTheDot extends Application{

//create ball for game
public static Circle dot;

//create pane to run game
public static Pane window;

//create score counter
int score = 0;

//creat random
Random r = new Random((21)+1);

@Override
public void start(Stage primaryStage) throws Exception {

    window.getChildren().addAll(dot);

    // create scene and place on pane
    Scene s = new Scene(window, 800, 800);
    primaryStage.setTitle("Catch The Dot");
    primaryStage.setScene(s);
    primaryStage.show();

    //move dot
    Timer timer = new Timer();
    timer.schedule(new TimerTask() 
        {
        @Override
        public void run(){
        window.getChildren().remove(dot);
        int randX = 1 + (int)(Math.random()*800);
        int randY = 1 + (int)(Math.random()*800);
        window.add(dot, randX, randY);
        }
        }, 0, 5000);        


    // create listener
    ActionEvent mouseClicked(ActionEvent event)
    {
        if(event.getSource()==dot)
        {
            score = score + 10;
            if(score == 50)
            {
                popUp(primaryStage);
            }
        }
    }
}

public void popUp(final Stage primaryStage)
{
    primaryStage.setTitle("You won!");
    final Popup popup = new Popup();
    popup.setX(300);
    popup.setY(200);
    Text t = new Text("You won! Nice job!");
    Text tt = new Text("Play again?");
    Button yes = new Button("yes");
    Button no = new Button("no");
    popup.getContent().addAll(t, tt, yes, no);
    yes.setOnAction(e -> Yes());
    no.setOnAction(e -> No());
}

public void Yes()
{
    restartGame();
}
public void No()
{
    System.exit(0);
}
public void restartGame()
{
    score = 0;

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

}我建议使用Math.random()代替


其余的看起来没问题。

你想让它跳到一个随机位置,还是让它继续移动?@FrankPuffer跳到一个随机位置好的,谢谢!我在“window.add(dot,ranX,ranY);”一行中得到一个错误,说这种类型的add对于这种类型的窗格是未定义的。你知道我应该使用什么样的窗格才能避免这个错误吗?我想这与窗口是一个窗格这一事实有关。我不认为“.add(foo-bar)”是API的一部分。我知道JFrame、JPanel和Canvas都有这个功能,可能就是您要找的。为了回答你应该使用哪一种,我想说画布是你最好的选择。好的,太好了!我要试试那些!谢谢你的帮助!下面是一个很好的系列文章,详细介绍了Java游戏编程:
Random random = new Random();
int ranX = random.nextInt(width-1); // random value from 0 to width
int ranY = random.nextInt(height-1);  // random value from 0 to height