I am printing objects from an array outputting 8 image objects using a for loop, the code works without draw() method, but when I include it, only the background image is shown.
How do I call the draw method in the setup method to work?
public class FishPAppletTest {
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; // fishes object, reference to perfect size array storing fish objects in the fish tank. These fish can be of different species.
private static Random randGen; // Type random constructor when new, Generator of random numbers
public static void main(String[] args) {
Utility.startApplication();
System.out.println(fishes[0]); //null because it is not stored?
}
/**
* 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 processing) { // PApplet is kept, processing is the variable as input
backgroundImage = processing.loadImage("images/background.png"); // loads image in background
fishes = new Fish[8];
processing.image(backgroundImage, processing.width / 2, processing.height /2); // draw background image
}
/**
* Draws and updates the application display window.
* This callback method called in an infinite loop.
*/
public static void draw() { // called by utility class
for (int i = 0; i < fishes.length; i++) {
fishes[i] = new Fish(processing, processing.width / 2, processing.height / 2);
randGen = new Random(); // WORKS UP
int x = randGen.nextInt(processing.width);
int y = randGen.nextInt(processing.height);
fishes[i] = new Fish(processing, x, y);
fishes[i].draw();
}
}
}
Without seeing what Utility and Fish do, it's hard to guess what the exact behaviour is.
If you're seing the background and not the fish it's likely
setup()
get's called, but notdraw()
for some reason.Ideally you'd initialise the
Fish
instances once insetup()
then re-use indraw()
(otherwise you're instantiating multiple times per second).I would try something simpler.
and this is a placeholder class mimicking your structure a bit:
Hopefully this helps restructure the program ? The ingredients are there, the order was a bit off.
FWIW, this is the sketch version you can simply paste and run in the Processing IDE: