# How to fix the code belowed in order to delete the leftmost ellipse when keyPressed

**URL:** https://discourse.processing.org/t/how-to-fix-the-code-belowed-in-order-to-delete-the-leftmost-ellipse-when-keypressed/29353
**Category:** Coding Questions
**Tags:** homework
**Created:** [April 14, 2021, 3:17am UTC](https://discourse.processing.org/t/how-to-fix-the-code-belowed-in-order-to-delete-the-leftmost-ellipse-when-keypressed/29353 "2021-04-14T03:17:54Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![batmm](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@batmm](https://discourse.processing.org/u/batmm)
#### Post date: [April 14, 2021, 3:17am UTC](https://discourse.processing.org/t/how-to-fix-the-code-belowed-in-order-to-delete-the-leftmost-ellipse-when-keypressed/29353/1 "2021-04-14T03:17:54Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)  
> \<final int SIZE = 20;

int x;//x-coordinates  
int y;//y-coordinates  
int mousePos;//keep track

void setup() {  
size(500, 500);

x = new int[1000];  
y = new int[1000];  
mousePos = 0;//initialize current position to 0  
}

void draw() {  
background(0);  
for (int i = 0; i \< mousePos; i++) {  
fill(255);  
ellipse(x[i], y[i], SIZE, SIZE);  
}  
}

void mouseClicked() {  
x[mousePos] = mouseX;  
y[mousePos] = mouseY;  
mousePos++;  
}

void keyPressed() {  
int minimum = x[0];  
for (int i = 1; i \< x.length; i++) {  
if (x[i] \< minimum) {  
minimum = x[i];  
}  
}  
if (minimum \>=0 && minimum \< x.length-1) {  
for (int i = minimum; i \< x.length-1; i++) {  
x[i] = x[i+1];  
}  
mousePos–;  
}  
}/\>

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [April 14, 2021, 9:04am UTC](https://discourse.processing.org/t/how-to-fix-the-code-belowed-in-order-to-delete-the-leftmost-ellipse-when-keypressed/29353/2 "2021-04-14T09:04:55Z")

</div>

Hi,

Storing your coordinates in a classic array might not be the easiest path to use.  
Indeed, you need to handle how to remove a value by sliding all the other values by one position in the array.

I would advise you to use an [arrayList](https://processing.org/reference/ArrayList.html) since you can use the `remove()` method to easily remove an element from your list.

And since you only want to remove the first element added, you could also use a [Queue](https://www.journaldev.com/13244/java-queue#java-queue-methods)

---

<div class="post-metadata">

### Author: ![batmm](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@batmm](https://discourse.processing.org/u/batmm)
#### Post date: [April 14, 2021, 3:44pm UTC](https://discourse.processing.org/t/how-to-fix-the-code-belowed-in-order-to-delete-the-leftmost-ellipse-when-keypressed/29353/3 "2021-04-14T15:44:45Z")

</div>

Another ways other than arrayList?

---

<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: [April 14, 2021, 3:48pm UTC](https://discourse.processing.org/t/how-to-fix-the-code-belowed-in-order-to-delete-the-leftmost-ellipse-when-keypressed/29353/4 "2021-04-14T15:48:15Z")

</div>

> [@jb4x](#):
>
> remove a value by sliding all the other values by one position in the array.

that’s the way.

use a for loop and copy each element one down

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [April 14, 2021, 4:14pm UTC](https://discourse.processing.org/t/how-to-fix-the-code-belowed-in-order-to-delete-the-leftmost-ellipse-when-keypressed/29353/5 "2021-04-14T16:14:14Z")

</div>

Why not using an arrayList?

---

<div class="post-metadata">

### Author: ![batmm](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@batmm](https://discourse.processing.org/u/batmm)
#### Post date: [April 14, 2021, 4:16pm UTC](https://discourse.processing.org/t/how-to-fix-the-code-belowed-in-order-to-delete-the-leftmost-ellipse-when-keypressed/29353/6 "2021-04-14T16:16:24Z")

</div>

I wish I could, but still a newbie and profs dont allow us to use.

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [April 14, 2021, 4:44pm UTC](https://discourse.processing.org/t/how-to-fix-the-code-belowed-in-order-to-delete-the-leftmost-ellipse-when-keypressed/29353/7 "2021-04-14T16:44:52Z")

</div>

If this is for an assignment, please tag your post as “Homework” then.

To get back at your code.  
First thing, consider using variable names that makes sense, it will help you a lot when working with longer piece of codes.  
For example, when I first read `mousePos` I thought it was used to store the x and y coordinates of the mouse while in fact it was referring to the number of ellipses that were drawn on the screen. So maybe a better name could be `ellipseNb`

You got the adding an ellipse part correctly.

I also realized, I did not read your question correctly… What you want is to get rid of the top left ellipse not the first one added.

Let’s take it step by step then.  
The first thing you want to do is to identified the top left ellipse. You already understand that for that you need to identify the one with the lower x value. **BUT** you also need to keep track of which one it is. So you actually need 2 variables: one to keep track of the minimum value and one to keep track of the index corresponding to that minimum value.  
Currently you are missing that last variable.

Then, once you have the index corresponding to the top left ellipse, you want to delete it. But since you are working with a simple array, you are not really deleting it, but more overriding its value. Imagine you have 5 ellipses and the following array:

```auto
x[0] = 20 
x[1] = 50
x[2] = 10
x[3] = 30
x[4] = 40
x[5] = ?? // Can be any value, you have only 5 ellipses so this value is not used 
x[6] = ?? // Can be any value, you have only 5 ellipses so this value is not used 
...
x[1000] = ?? // Can be any value, you have only 5 ellipses so this value is not used 

```

If you have only 5 ellipses then you also have `ellipseNb = 5` so you can do this in draw:

```auto
for (int i = 0; i < ellipseNb; i++) {
  fill(255);
  ellipse(x[i], y[i], SIZE, SIZE);
}

```

As you can see, ellipse with index 2 is the most left one, so you want to “remove it”. Well first, you know that you will only have 4 ellipse remaining, so you can already write `ellipseNb = 4`  
Because of this you will only draw ellipses 0 through 3 so what you want to have is:

```auto
x[0] = 20 
x[1] = 50
x[2] = 30 // Previous x[3]
x[3] = 40 // Previous x[4]
x[4] = ?? // Can be any value, you have only 5 ellipses so this value is not used 
x[5] = ?? // Can be any value, you have only 5 ellipses so this value is not used 
x[6] = ?? // Can be any value, you have only 5 ellipses so this value is not used 
...
x[1000] = ?? // Can be any value, you have only 5 ellipses so this value is not used 

```

So all you have to do is slide by one each value starting at the index of the most left ellipse up to the number of ellipses left.

I think you have enough element to start working out a solution now.
