Drawing order of Objects in an Arraylist

Remark

I wonder: it shouldn’t matter whether you placed LAWN first or a Stone, the stone should always be on top, because its Z is higher.

I am referring to this code:

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

Here the order of the placing of the garden objects is irrelevant, it’s about that a stone receives the default Z for a stone from the stoneZ variable.


Your Question

Of course: just search the minimum instead of the maximum

//Remove clicked Object
void mouseClicked() {

  if (ButtonState == 4) {

    int minZ= 10000;  // store the smallest Z as minZ (starting with a high value, so the first item is definitely smaller)
    int minZ_Index = -1;  // store the index of the garden object with the smallest Z (as minZ_Index)

    // search highest Z when multiple garden objects are underneath the mouse  
    for (int i=0; i<gardenobjects.size(); i++) {
      if (gardenobjects.get(i).isInside(mouseX, mouseY)) {
        if (gardenobjects.get(i).z < minZ) {   // we use smaller here:   < 
          minZ = gardenobjects.get(i).z; // store 
          minZ_Index = i;
        }//if
      }//if
    }//for

    // -----

    // Evaluate the findings 
    //after the loop delete the garden object with this index.
    if (minZ_Index > -1) { // found something?
      gardenobjects.remove(minZ_Index); // Yes
    }//if
    //
  }//if
}//function

Remark 2

When you have a grid of tiles / cells as your garden, you would always have only one item per cell, lawn OR stone. Then would we discuss would be unnecessary. But you would have another model how you store the data of your garden.

Remark 3

I wonder how you place garden objects.
At the moment you cannot press and hold the mouse and paint an entire section with lawn, right? Instead you have to click, release, click release and fill the garden with lawn? That might be difficult.

Warm regards,

Chrisir

This is maybe not a question of the Z value but of the dragging itself.

While you drag you should set a boolean dragging to true.

void mousePressed() {
    ........
    ........
    dragging = true; 
}

When dragging is true, your program should not be allowed to pick up another piece at all (use an if-clause like if(! dragging) { … before start dragging ). No matter what the Z is.

and then

void mouseReleased() {
    dragging = false; 
}

Chrisir