【发布时间】: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