How to call method with for loop in another method?

99 Views Asked by At

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();
    }
  }
}
1

There are 1 best solutions below

0
On

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 not draw() for some reason.

Ideally you'd initialise the Fish instances once in setup() then re-use in draw() (otherwise you're instantiating multiple times per second).

I would try something simpler.

public class FishPAppletTest extends PApplet{

  private PImage backgroundImage; // PImage object that represents the background image

  private Fish[] fishes;

   // fishes object, reference to perfect size array storing fish objects in the fish tank. These fish can be of different species.

  public void setup() { 
    // wild guess about sketch dimensions
    size(600, 600);

    backgroundImage = loadImage("images/background.png"); 
    //loads image in background
    
    fishes = new Fish[8];

    for (int i = 0; i < fishes.length; i++) {

      fishes[i] = new Fish(this, width / 2, height / 2); 

    }

  }
      
  /**
  * Draws and updates the application display window.
  * This callback method called in an infinite loop.
  */

  public void draw() { 

    image(backgroundImage, width / 2, height /2);

    for (int i = 0; i < fishes.length; i++) {

    
      // hopefully Fish has public x,y properties ?
      fishes[i].x = (int)random(width);
      fishes[i].y = (int)random(height);
      fishes[i].draw();

    }
   
  }
  // initialise PApplet (kicks off setup/draw Processing sketch lifecycle)
  public static void main(String[] args) {

    PApplet.main(FishPAppletTest.class.getCanonicalName());
    
  }

}

and this is a placeholder class mimicking your structure a bit:

public class Fish{

  private PAppet parent;
  public float x, y;

  public Fish(PApplet parent, float x, float y){
    this.parent = parent;
    this.x = x;
    this.y = y;
  }   

  public void draw(){
    parent.ellipse(x, y, 20, 10);
  }


}

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:

// PImage object that represents the background image
private PImage backgroundImage; 

// fishes reference to perfect size array storing fish objects in the fish tank. These fish can be of different species.
private Fish[] fishes;


public void setup() { 
  size(600, 600);
  
  //loads image in background
  backgroundImage = loadImage("images/background.png"); 
  
  fishes = new Fish[8];
  // instantiate Fish once
  for (int i = 0; i < fishes.length; i++) {

    fishes[i] = new Fish(this, width / 2, height / 2); 

  }

}
    
/**
* Draws and updates the application display window.
* This callback method called in an infinite loop.
*/

public void draw() { 

  image(backgroundImage, 0, 0);
  
  // reuse Fish instances
  for (int i = 0; i < fishes.length; i++) {

    // hopefully Fish has public x,y properties ?
    fishes[i].x = (int)random(width);
    fishes[i].y = (int)random(height);
    fishes[i].draw();

  }
 
}

public class Fish{

  private PApplet parent;
  public float x, y;

  public Fish(PApplet parent, float x, float y){
    this.parent = parent;
    this.x = x;
    this.y = y;
  }   

  public void draw(){
    parent.ellipse(x, y, 20, 10);
  }


}