【问题标题】:Drawing multiple fish at random locations在随机位置绘制多条鱼
【发布时间】:2021-10-03 16:17:30
【问题描述】:

我正在尝试创建一个 java 项目,其中包含一个随机位置最多可容纳 8 条鱼的水族馆。鱼只能在用鼠标拖动时移动。我们使用了水族馆背景和鱼的图像。我现在可以在中心创建一条鱼,现在需要在随机位置绘制多条鱼(最多八条)。

这是我的任务

现在,我们想在鱼缸中添加最多 8 条鱼并绘制它们 在窗口显示的随机位置。生成这些随机 位置,我们将使用 randGen 类型的静态变量 我们已经在任何方法之外定义的随机数。首先,确保 在 setup() 方法中创建一个 Random 对象并分配其 引用静态变量 randGen。 (float)randGen.nextInt(processing.width) // 生成一个随机数 x-position of type // 在显示窗口的宽度内浮动 (float)randGen.nextInt(processing.height) // 生成一个随机数 y-position of type // 在显示窗口的高度内浮动 To 在显示窗口的给定位置创建一条鱼,调用 Fish 类的构造函数,它接受三个输入参数 鱼(PApplet,浮动,浮动)。然后,您可以存储 由 Fish 构造函数调用返回的创建的 Fish 对象 数组鱼的位置 0。接下来可以添加一个for循环来绘制 中的每个非空引用指向的 Fish 对象 通过调用每个 Fish 对象的 draw() 方法来创建 fishes 数组 适当地。完成此操作后,您现在可以使用 显示中随机位置的不同数量的鱼 窗口,并带有不同大小的鱼数组。在继续之前 到下一步,让 fishes 引用一个长度为 8 的数组。制作 除了第一个引用之外的所有引用都为空,并使第一个引用 引用位于随机位置的 Fish 对象 屏幕。”

这是我目前的代码:

import java.util.Random;
import java.io.File;
import processing.core.PApplet;
import processing.core.PImage;
public class FishTank {
    private static PApplet processing; // PApplet object that represents the graphic
                                       // interface of the JunglePark application
    private static PImage backgroundImage;  // PImage object that represents the
                                            // background image
    private static Fish[] fishes;   // perfect size array storing the different fish present
                                    // in the fish tank.  These fish can be different species.
    private static Random randGen;  // generator of random numbers.
    public static void main(String[] args) {
    Utility.startApplication();  //starts the application
    }
    /**
     * Defines the initial environment properties of this application
     * @param processingOBJ a reference to the graphic display window of this application
     */
    public static void setup(PApplet processingOBJ) {   
        processing = processingOBJ;
        // load the image of the background
        backgroundImage = processing.loadImage("images/background.png");
        //Draw the background image at the center of the screen
        processing.image(backgroundImage,  processing.width / 2,  processing.height / 2);
        // width [resp.height]  System variable of the processing library that stores
        // the width [resp. height] of the display window.
        fishes = new Fish[8]; 
        fishes[0] = new Fish(processing, processing.width / 2, processing.height / 2);
        Random randGen = new Random();
        //float num = randGen.nextInt(processing.width); //generates a random x-position of type
                                         //float within the width of the display window.
        //float num1 = randGen.nextInt(processing.height);
    }
    
    /**
     * Draws and updates the application display window.
     * This callback method called in an infinite loop
     */
    public static void draw() {
        for (int i =0; i < fishes.length; i++) {
            if (fishes[i] != null) {
                fishes [i].draw(); // where i is the index of the created Fish in the fishes array
            }
        }
    }
    /**
     * Callback method called each time the user presses the mouse.
     */
    public static void mousePressed() {
        
        }
    
    /**
     * Callback method called each time the mouse is released
     */
    public static void mouseReleased() {
        
    }
    
    public static void keyPressed() {
        
    }
    /**
     * Checks if the mouse is over a specific Fish whose reference is provided
     * as input parameter
     * 
     * @param Fish reference to a specific fish
     * @return true if the mouse is over the specific Fish object (i.e. over 
     *           the image of the Fish), Fallse otherwise
     */
    public static boolean isMouseOver(Fish fish) {
        float X = fish.getPositionX();
        float Y = fish.getPositionY();
        if ((X - fish.getImage().width/2) < processing.mouseX && processing.mouseX < (X + fish.getImage().width/2) &&
        (Y - fish.getImage().height/2) < processing.mouseY && processing.mouseY < (Y + fish.getImage().height/2))
        {
            return true;  //If mouse is over the boundaries of fish returns true
        }
        else {
            return false;
        }
    }
}

【问题讨论】:

  • 您的问题是什么? “这是我的任务,这是我的代码”不是问题。 :(
  • 最好尝试指定您需要的内容。否则,如果你不改进,这个问题可能会被删除。

标签: java processing


【解决方案1】:

您已经实现了大部分需要的功能。您还成功地在硬编码位置(中心)创建了至少一条鱼。我们可以从你初始化fish数组的地方开始。

创建长度为 8 的鱼数组后,我们只需在随机位置填充鱼。

要获得随机值,您的分配状态

[...] 首先,确保在 setup() 方法中创建一个 Random 对象,并将其引用分配给静态变量 randGen [...]

您确实这样做了,但是您创建了一个 local 变量,而不是关联到 static 变量。因此,您需要编写 randGen = new Random(); 而不是 Random randGen = new Random();,就像使用 fishes 一样。

为此,for 循环可能是最明智的选择(如在 draw 方法中所见),我们使用随机对象生成的位置创建鱼。

来自你的任务

[...] (float) randGen.nextInt(processing.width) // 在显示窗口宽度内生成浮点类型的随机 x 位置 (float) randGen.nextInt(processing.height) //生成类型的随机 y 位置 // 在显示窗口的高度内浮动 [...]

[...]

fishes = new Fish[8];
randGen = new Random();

for (int i = 0; i < fishes.length; i++) {
    float x = (float) randGen.nextInt(processing.width);
    float y = (float) randGen.nextInt(processing.height);
    fishes[i] = new Fish(processing, x, y);
}

[...]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 2015-07-21
    • 2013-09-07
    相关资源
    最近更新 更多