java - 让球不断出现在不同的位置

标签 java

我是一个Java初学者,我正在尝试编写一个程序,其中白球不断出现在屏幕上。一个球出现在下一个球之前应该有几秒钟的停顿,并且球需要出现在屏幕上的不同位置。我需要帮助使用 RandomGenerator 使球出现在不同的地方。任何帮助将不胜感激!

私有(private)RandomGenerator rgen = new RandomGenerator();

//~ Constructor ...........................................................

// ----------------------------------------------------------
/**
 * Creates a new BubbleGame object.
 */
public void init()
{
    //call method to create regions
    CreateRegions();

    //add mouse listeners
    addMouseListeners();

    //loop to add bubbles
    while (true)
    {
        //create a filled bubble
        GOval bubble = new GOval (100, 100, 50, 50);
        bubble.setFilled(true);
        bubble.setColor(Color.WHITE);

        //randomly generate coordinates within the field
        int rgen = 

        //add the bubble and pause 
        add(bubble);
        Thread.sleep(3000);

    }
}

最佳答案

您可以使用Random.nextInt(maxInt)生成0到maxInt-1之间的数字。

Random rnd = new Random();
rnd.nextInt(10); // 0-9

关于java - 让球不断出现在不同的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19995915/

相关文章:

java - 将数据结构从 java 传递到 perl(反之亦然)

java - 用 Java 创建自然 DSL 的最佳工具是什么?

java - Android 照片分享与 FileProvider

java - 方法不存在 (MainActivity.java)

java - Logback 仅在文件大小上滚动

java - 将 1GB 数据加载到 hbase 需要 1 小时

java - android.view.InflateException : Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>

java - Retrofit 不调用 failure 并且 response.code() 是 400

java - 服务器IPC版本9在访问hdfs时无法与客户端通信

java - 尽管指定了 'key-type',但 Spring 3.2.4 无法在 <util :map. ../> 中将 String 转换为 Enum