# Drawing order of Objects in an Arraylist

**URL:** <https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160>\
**Category:** Beginners\
**Created:** [January 18, 2021, 3:57pm UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160 "2021-01-18T15:57:29Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 19, 2021, 9:01pm UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/21 "2021-01-19T21:01:16Z")

</div>

**Remark**

> [@Gine6g](#):
>
> because right now when two of the same objects are above each other it deletes the one that has been placed first which is the bottom one

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:

```auto
    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**

> [@Gine6g](#):
>
> if its possible to walk through that zMax loop the other way around?

Of course: just search the minimum instead of the maximum

```auto
//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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 19, 2021, 9:05pm UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/22 "2021-01-19T21:05:41Z")

</div>

> [@Gine6g](#):
>
> when I drag one Object over another with the same Z value which has been created before the one I’m dragging it switches over that one and drags that

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.

```auto
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

```auto
void mouseReleased() {
    dragging = false; 
}

```

Chrisir

[Previous page](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160.md?page=1)
