# 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:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Gine6g](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@Gine6g](https://discourse.processing.org/u/Gine6g)
#### Post date: [January 18, 2021, 3:57pm UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/1 "2021-01-18T15:57:29Z")

</div>

I didnt knew how to name this. But I got a class with multiple other classes that inherit from the main one.

Now when I clicking I can add one of the Objects to the Sketch and all the Objects are held withing an Arraylist.

The problem I have, is that I want to have them in a specific order. Lets say first I create a “Stone”. Now I want to create a “Lawn” underneath that stone. But when I click to create a Lawn its on top of the stone.

How can I fix that?

Here are the important parts of my code:

```auto
void setup () {
  size (1000, 700);

  gardenobjects = new ArrayList<GardenObject>(); //creates Arraylist
}

```

```auto

  //Add new Stone on click
  if (ButtonState == 1 && Gardening == true) {

    gardenobjects.add(new Stone(mouseX, mouseY, 50));
  }

void mousePressed() {
 //Add new Stone on click
  if (ButtonState == 1 && Gardening == true) {

    gardenobjects.add(new Stone(mouseX, mouseY, 50));
  }

  //Add new Lawn on click
  if (ButtonState == 2 && Gardening == true) {
    gardenobjects.add(new Lawn(mouseX, mouseY, 100));
  }
}

```

```auto
void draw() {

  for (int i=0; i<gardenobjects.size(); i++) {
    GardenObject g = gardenobjects.get(i);
    g.draw();
  }
}

```

let me know If you need more information of the code.

---

<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 18, 2021, 4:07pm UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/2 "2021-01-18T16:07:46Z")

</div>

The Lawn is underneath the stone when you first draw the lawn and then the stone.

You could give every object an Z value and

- loop over the entire ArrayList and first draw all with 0 Z then
- loop over the entire ArrayList again and draw all with Z 1
- etc.

Or when you only have a small numbers of types, then just do the same and draw Lawn first, then draw Stone etc.

\*\*Hey, and welcome to the forum! \*\*

**Great to have you here!**

---

<div class="post-metadata">

### Author: ![Gine6g](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@Gine6g](https://discourse.processing.org/u/Gine6g)
#### Post date: [January 18, 2021, 4:10pm UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/3 "2021-01-18T16:10:47Z")

</div>

Thanks for the quick Answer!

But I think im not sure how to do that. How would I give Lawn for example Z=0 and Stone Z=1?

and how would I tell the programm to first draw the 0s and then the 1s?

---

<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 18, 2021, 4:20pm UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/4 "2021-01-18T16:20:42Z")

</div>

let’s say you have an String type in the class

This is either “lawn”, “stone” etc.

before setup()

```auto
// Their ORDER must be from draw first to draw LAST
String[] typeList = {
"lawn", 
"stone",
"sand"
};

```

DRAW

```auto
for(String s : typeList) {
   for (GardenObject g = gardenobjects) {
       if(g.type.equals(s)) {
            g.draw();
       }
   }
}

```

---

<div class="post-metadata">

### Author: ![Gine6g](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@Gine6g](https://discourse.processing.org/u/Gine6g)
#### Post date: [January 18, 2021, 4:24pm UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/5 "2021-01-18T16:24:56Z")

</div>

Ahh thanks i think i understand that now. Though i dont think we are allowed to use that yet. We only got Arrays and Arraylists thats why I am confused as well because in our Lectures we havent discussed anything that could let me know how to achieve what I want yet we need to do that 😃

---

<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 18, 2021, 4:43pm UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/6 "2021-01-18T16:43:35Z")

</div>

> [@Gine6g](#):
>
> we havent discussed anything that could let me know how to achieve what I want yet we

I only use Array / ArrayList and String

(and a short form of a for-loop, it works like **for each String “s” in `typeList` do {…}** )

---

<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 18, 2021, 7:47pm UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/7 "2021-01-18T19:47:33Z")

</div>

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

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

```

**DRAW**

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

```

---

<div class="post-metadata">

### Author: ![Gine6g](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@Gine6g](https://discourse.processing.org/u/Gine6g)
#### Post date: [January 19, 2021, 9:04am UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/8 "2021-01-19T09:04:13Z")

</div>

Wow thanks yea that will work. I hope I understand it. I will try later.

This could also fix another problem I guess. Because I need to make it so that I can delete objects with a click as well. Currently when for example the grass is underneath a stone the grass also is deleted. But I think this way I can prevent it from happening.

---

<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:28am UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/9 "2021-01-19T09:28:10Z")

</div>

**Remark 1**

instead of going over the entire list and each time search for a Z it would be faster to sort the entire list by Z and then just display it in the right Z order. But sorting in my experience is hard for a beginner

**Remark 2**

> [@Gine6g](#):
>
> I need to make it so that I can delete objects with a click as well. Currently when for example the grass is underneath a stone the grass also is deleted. But I think this way I can prevent it from happening.

I am not sure this will prevent that.

If not:  
Instead when you click on a field and there are e.g. three garden objects underneath, you have to check which one of those has the highest Z and delete only this one.

Which is in itself a bit tricky. Rough description: You need to store the highest Z as `maxZ` and compare against this and also store the **index** of the garden object with the highest Z (`maxZ_Index`) and _after_ the loop delete the garden object with this index.

---

<div class="post-metadata">

### Author: ![Gine6g](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@Gine6g](https://discourse.processing.org/u/Gine6g)
#### Post date: [January 19, 2021, 9:31am UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/10 "2021-01-19T09:31:05Z")

</div>

Yea, efficiency isnt important yet. But thanks for now I see if I can make it like that.

May I ask you if a problem occurs i cant fix?

---

<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:32am UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/11 "2021-01-19T09:32:18Z")

</div>

Hey, of course you can!

---

<div class="post-metadata">

### Author: ![Gine6g](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@Gine6g](https://discourse.processing.org/u/Gine6g)
#### Post date: [January 19, 2021, 10:09am UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/12 "2021-01-19T10:09:13Z")

</div>

I didn’t try it yet. But I see you wrote g.z == allowed

Wouldn’t that g.z mean that I need to make a new function inside of the class?

---

<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, 10:11am UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/13 "2021-01-19T10:11:19Z")

</div>

allowedZ is just the variable in the first for loop

You need to implement z in the class

---

<div class="post-metadata">

### Author: ![Gine6g](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@Gine6g](https://discourse.processing.org/u/Gine6g)
#### Post date: [January 19, 2021, 10:15am UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/14 "2021-01-19T10:15:11Z")

</div>

Yea, is it as simple as implementing Z as the last variable of the Constructor so in my Stone class wich extends the GardenObject class I put:

```auto
  Stone(..., ..., ..., int z) {

    ...
    ...
    ...
    z =1
  }

```

---

<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, 10:29am UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/15 "2021-01-19T10:29:28Z")

</div>

I think so, yeah.

z must be in all classes and have the right value

---

<div class="post-metadata">

### Author: ![Gine6g](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@Gine6g](https://discourse.processing.org/u/Gine6g)
#### Post date: [January 19, 2021, 10:32am UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/16 "2021-01-19T10:32:31Z")

</div>

It worked thank you so much that actually helped me understand all of this a lot more.

Now im gonna figgure how i do the deleting part

---

<div class="post-metadata">

### Author: ![Gine6g](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@Gine6g](https://discourse.processing.org/u/Gine6g)
#### Post date: [January 19, 2021, 11:39am UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/17 "2021-01-19T11:39:39Z")

</div>

It seems that I cant sort out how to use Z to define wich object im clicking…

for the detection if my mouse is over i use a Boolean inside the main class “Garden Object”

```auto
 boolean isInside (float mx, float my) {
    return x-s/2<=mx && mx<=x+s/2 &&
      y-s/2<=my && my<=y+s/2;
  }

```

x and y are the positions, s is the size and mx and my would be mouseX and mouseY.

to actually delete the Object im currently using this in the main program:

```auto
//Remove clicked Object
void mouseClicked() {

  if (ButtonState == 4) {

    for (int i=0; i<gardenobjects.size(); i++) {
      if (gardenobjects.get(i).isInside(mouseX, mouseY)) {
        gardenobjects.remove(i);
        i--;
      }
    }
  }
}

```

how could I use Z in there to only delete the first object my mouse is over?

---

<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, 2:12pm UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/18 "2021-01-19T14:12:12Z")

</div>

That was what I tried to describe before with `maxZ` and so on.

Let me see…

We first loop over the entire thing and find the clicked tile with the highest Z.

Then after the for-loop we remove it.

(can’t test it here…)

```auto

//Remove clicked Object
void mouseClicked() {

  if (ButtonState == 4) {

    int maxZ= -100; // store the highest Z as maxZ
    int maxZ_Index = -1; // store the index of the garden object with the highest Z (as maxZ_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 > maxZ) {
          maxZ = gardenobjects.get(i).z; // store 
          maxZ_Index = i;
        }//if
      }//if
    }//for

    // -----

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

```

(by the way when drawing the garden objects: I wrote 4 there in the for-loop please replace with how many Z-values you really have (this +1, so when you have 0 and 1 as Z-values, you must go to \< 2))

---

<div class="post-metadata">

### Author: ![Gine6g](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@Gine6g](https://discourse.processing.org/u/Gine6g)
#### Post date: [January 19, 2021, 2:38pm UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/19 "2021-01-19T14:38:20Z")

</div>

I’m gonna test that later this day. Thank you very much sir.

Yea 4 is actually already the proper number.

---

<div class="post-metadata">

### Author: ![Gine6g](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@Gine6g](https://discourse.processing.org/u/Gine6g)
#### Post date: [January 19, 2021, 8:11pm UTC](https://discourse.processing.org/t/drawing-order-of-objects-in-an-arraylist/27160/20 "2021-01-19T20:11:09Z")

</div>

the code definetely works. What I am wondering now if its possible to walk through that zMax loop the other way around? because right now when two of the same objects are above each other it deletes the one that has been placed first wich is the bottom one.

I also used your example to implement a drag and drop functionality. And the same problem occurs, because when I drag one Object over another with the same Z value wich has been created before the one im dragging it switches over that one and drags that

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