Drawing order of Objects in an Arraylist

Of course you can also use your idea with a z value

Use a nested for loop like I did above and instead of checking for a String check for the Z

Then you need to bring a Z-Value into the class

    zValue = stoneZ;  // when you define Z values for stone and Lawn before setup()
    gardenobjects.add(new Stone(mouseX, mouseY, 50, zValue )); // change your constructor

DRAW

// here all Garden Objects with Z == 0 are drawn first, then all those with Z ==1, then Z==2 etc.
for(int allowedZ = 0; allowedZ < 4; allowedZ++) { // 4 is just a guess. 
   for (GardenObject g : gardenobjects) { // I had an error here first 
       if(g.z == allowedZ) { // check Z 
            g.draw();              // draw 
       }
   }
}
1 Like