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

please format code with </> button * homework policy * asking questions
<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–;
}
}/>

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

3 Likes

Another ways other than arrayList?

that’s the way.

use a for loop and copy each element one down

Why not using an arrayList?

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

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:

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:

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:

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.

1 Like