# How can i make the size of ellipse keeps the size after i released the mouse

**URL:** https://discourse.processing.org/t/how-can-i-make-the-size-of-ellipse-keeps-the-size-after-i-released-the-mouse/39694
**Category:** Coding Questions
**Tags:** homework
**Created:** [November 13, 2022, 7:22pm UTC](https://discourse.processing.org/t/how-can-i-make-the-size-of-ellipse-keeps-the-size-after-i-released-the-mouse/39694 "2022-11-13T19:22:19Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![returng](https://avatars.discourse-cdn.com/v4/letter/r/97f17d/32.png) [@returng](https://discourse.processing.org/u/returng)
#### Post date: [November 13, 2022, 7:22pm UTC](https://discourse.processing.org/t/how-can-i-make-the-size-of-ellipse-keeps-the-size-after-i-released-the-mouse/39694/1 "2022-11-13T19:22:19Z")

</div>

My task it that I have to make the size of the ellipse become smaller as I use mousePressed, but then I need it to keep the size after I released the mouse, not the original size.

class Particle{  
float x; //X Position  
float y; //Y Position  
float velX; //velocity on X axis  
float velY; //velocity on Y axis  
float size; // Size of the particle  
float lifespan; // duration of the particle  
float col;//create fire type colours

Particle(float x,float y){  
this.velY = random(-10,10);  
this.velX = random(-10,10);  
this.x = x;  
this.y = y;  
this.size = 20;  
lifespan = 220.0;  
col = random(#DE8816);  
}

void update(float gravity){  
this.x += velX; // direction of X axis  
velY += gravity; // gravity affectiing the velocity on Y axis causing to fall  
this.y += velY; //direction of Y axis  
lifespan -= 1.0;  
if (mousePressed ) {  
size = size - 0.5;  
}  
if (size\<0){  
size = 1;  
}

}

boolean isDead() {  
if (lifespan \< 0.0) {  
return true;  
} else {  
return false;  
}  
}

void draw() {  
stroke(0,lifespan);  
fill(#DE8816,lifespan);  
ellipse(x,y,size,size);  
}

}

---

<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: [November 13, 2022, 9:10pm UTC](https://discourse.processing.org/t/how-can-i-make-the-size-of-ellipse-keeps-the-size-after-i-released-the-mouse/39694/2 "2022-11-13T21:10:25Z")

</div>

> [@returng](#):
>
> this.size = 20;

Maybe this is not enough, say 90?

The code looks okay; please post rest of the sketch, maybe the error is there?

For example you should not use new Particle in draw()…

---

<div class="post-metadata">

### Author: ![returng](https://avatars.discourse-cdn.com/v4/letter/r/97f17d/32.png) [@returng](https://discourse.processing.org/u/returng)
#### Post date: [November 13, 2022, 9:21pm UTC](https://discourse.processing.org/t/how-can-i-make-the-size-of-ellipse-keeps-the-size-after-i-released-the-mouse/39694/3 "2022-11-13T21:21:57Z")

</div>

Here is the full code, thank you

float gravity = .4;  
int particle\_each\_time = 4;  
ArrayList particle= new ArrayList(); // ArrayList to manage to list of all particles

void setup() {  
size(640,480);  
}

void draw() {  
background(0);  
for (int i = 0; i \< particle\_each\_time; i++) {  
particle.add(new Particle(mouseX,mouseY));  
}  
for (int i = 0; i \< particle.size(); i++) {  
Particle p = particle.get(i);  
p.update(gravity);  
p.draw();  
}

}

class Particle{  
float x; //X Position  
float y; //Y Position  
float velX; //velocity on X axis  
float velY; //velocity on Y axis  
float size; // Size of the particle  
float lifespan; // duration of the particle  
float col;//create fire type colours

Particle(float x,float y){  
this.velY = random(-10,10);  
this.velX = random(-10,10);  
this.x = x;  
this.y = y;  
this.size = 20;  
lifespan = 220.0;  
col = random(#DE8816);  
}

void update(float gravity){  
this.x += velX; // direction of X axis  
velY += gravity; // gravity affectiing the velocity on Y axis causing to fall  
this.y += velY; //direction of Y axis  
lifespan -= 1.0;  
if (mousePressed ) {  
size = size - 0.5;  
} else  
if (size\<0){  
size = 1;  
}

}

boolean isDead() {  
if (lifespan \< 0.0) {  
return true;  
} else {  
return false;  
}  
}

void draw() {  
stroke(0,lifespan);  
fill(#DE8816,lifespan);  
ellipse(x,y,size,size);  
}

}

---

<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: [November 13, 2022, 9:45pm UTC](https://discourse.processing.org/t/how-can-i-make-the-size-of-ellipse-keeps-the-size-after-i-released-the-mouse/39694/4 "2022-11-13T21:45:19Z")

</div>

> [@returng](#):
>
> if (mousePressed ) {  
> size = size - 0.5;  
> } else  
> if (size\<0){  
> size = 1;  
> }

Hmm.

Remove the else here - just the word else, not more

Replace 0.5 with 0.01

---

<div class="post-metadata">

### Author: ![returng](https://avatars.discourse-cdn.com/v4/letter/r/97f17d/32.png) [@returng](https://discourse.processing.org/u/returng)
#### Post date: [November 16, 2022, 9:33am UTC](https://discourse.processing.org/t/how-can-i-make-the-size-of-ellipse-keeps-the-size-after-i-released-the-mouse/39694/5 "2022-11-16T09:33:46Z")

</div>

sorry, it still the same thing.  
what i need to do is:

- As long as the mouse is still pressed, the shapes get smaller and smaller with each frame (but they never vanish).
- When the mouse is released, the shapes keep their size.
- When the mouse is clicked again, the „explosion“ is reset (i.e. all shapes that are still visible from the previous explosion vanish and the explosion starts again).

the first point is done, only need to do the second one and have no idea how

---

<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: [November 16, 2022, 10:07am UTC](https://discourse.processing.org/t/how-can-i-make-the-size-of-ellipse-keeps-the-size-after-i-released-the-mouse/39694/6 "2022-11-16T10:07:40Z")

</div>

Post your entire code please

---

<div class="post-metadata">

### Author: ![returng](https://avatars.discourse-cdn.com/v4/letter/r/97f17d/32.png) [@returng](https://discourse.processing.org/u/returng)
#### Post date: [November 16, 2022, 10:26am UTC](https://discourse.processing.org/t/how-can-i-make-the-size-of-ellipse-keeps-the-size-after-i-released-the-mouse/39694/7 "2022-11-16T10:26:33Z")

</div>

float gravity = .4;  
int particle\_each\_time = 4;  
ArrayList particle= new ArrayList(); // ArrayList to manage to list of all particles

void setup() {  
size(640,480);  
}

void draw() {  
background(0);  
for (int i = 0; i \< particle\_each\_time; i++) {  
particle.add(new Particle(mouseX,mouseY));  
}  
for (int i = 0; i \< particle.size(); i++) {  
Particle p = particle.get(i);  
p.update(gravity);  
p.draw();  
}

}

//Next tab

class Particle{  
float x; //X Position  
float y; //Y Position  
float velX; //velocity on X axis  
float velY; //velocity on Y axis  
float size; // Size of the particle  
float lifespan; // duration of the particle  
float col;//create fire type colours

Particle(float x,float y){  
this.velY = random(-10,10);  
this.velX = random(-10,10);  
this.x = x;  
this.y = y;  
this.size = 20;  
lifespan = 220.0;  
col = random(#DE8816);  
}

void update(float gravity){  
this.x += velX; // direction of X axis  
velY += gravity; // gravity affectiing the velocity on Y axis causing to fall  
this.y += velY; //direction of Y axis  
lifespan -= 1.0;  
if (mousePressed ) {  
size = size - 0.5;  
} else  
if (size\<0){  
size = 1;  
}

}

boolean isDead() {  
if (lifespan \< 0.0) {  
return true;  
} else {  
return false;  
}  
}

void draw() {  
stroke(0,lifespan);  
fill(#DE8816,lifespan);  
ellipse(x,y,size,size);  
}

}

---

<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: [November 16, 2022, 10:42am UTC](https://discourse.processing.org/t/how-can-i-make-the-size-of-ellipse-keeps-the-size-after-i-released-the-mouse/39694/8 "2022-11-16T10:42:20Z")

</div>

Thank you!

here the particles keep their size when you release the mouse.

but only the old particles (which leave the screen soon), not the newly spawned ones. Do you mean the old ones or also the new ones?

To change new ones, change the size in the constructor Particle(float x, float y) {

- Also consider removing old particles from the ArrayList using isDead

```auto

float gravity = .4;
int particle_each_time = 4;
ArrayList<Particle> particle= new ArrayList(); // ArrayList to manage to list of all particles

void setup() {
  size(640, 480);
}

void draw() {
  background(0);
  for (int i = 0; i < particle_each_time; i++) {
    particle.add(new Particle(mouseX, mouseY));
  }
  for (int i = 0; i < particle.size(); i++) {
    Particle p = particle.get(i);
    p.update(gravity);
    p.draw();
  }
}

//Next tab

class Particle {
  float x; //X Position
  float y; //Y Position
  float velX; //velocity on X axis
  float velY; //velocity on Y axis

  float size; // Size of the particle
  // float sizeFromMouse; 

  float lifespan; // duration of the particle
  float col;//create fire type colours

  Particle(float x, float y) {
    this.velY = random(-10, 10);
    this.velX = random(-10, 10);
    this.x = x;
    this.y = y;
    this.size = 20;
    lifespan = 220.0;
    col = random(#DE8816);
  }

  void update(float gravity) {
    this.x += velX; // direction of X axis
    velY += gravity; // gravity affectiing the velocity on Y axis causing to fall
    this.y += velY; //direction of Y axis
    lifespan -= 1.0;
    if (mousePressed ) {
      size = size - 0.51;
    } 

    if (size<0) {
      size = 1;
    }
  }

  boolean isDead() {
    if (lifespan < 0.0) {
      return true;
    } else {
      return false;
    }
  }

  void draw() {
    stroke(0, lifespan);
    fill(#DE8816, lifespan);
    ellipse(x, y, size, size);
  }
}

```

Here is a new version with removing old ones from the list

```auto

float gravity = .4;
int particle_each_time = 4;
ArrayList<Particle> particle= new ArrayList(); // ArrayList to manage to list of all particles

void setup() {
  size(640, 480);
}

void draw() {
  background(0);

  for (int i = 0; i < particle_each_time; i++) {
    particle.add(new Particle(mouseX, mouseY));
  }

  for (int i = 0; i < particle.size(); i++) {
    Particle p = particle.get(i);
    p.update(gravity);
    p.draw();
  }

  for (int i = particle.size()-1; i >= 0; i--) {
    Particle p = particle.get(i);
    if (p.isDead()) 
      particle.remove(i);
  }
}

//Next tab

class Particle {
  float x; //X Position
  float y; //Y Position
  float velX; //velocity on X axis
  float velY; //velocity on Y axis

  float size; // Size of the particle
  // float sizeFromMouse; 

  float lifespan; // duration of the particle
  float col;//create fire type colours

  Particle(float x, float y) {
    this.velY = random(-10, 10);
    this.velX = random(-10, 10);
    this.x = x;
    this.y = y;
    this.size = 20;
    lifespan = 220.0;
    col = random(#DE8816);
  }

  void update(float gravity) {
    this.x += velX; // direction of X axis
    velY += gravity; // gravity affectiing the velocity on Y axis causing to fall
    this.y += velY; //direction of Y axis
    lifespan -= 1.0;
    if (mousePressed ) {
      size = size - 0.51;
    } 

    if (size<0) {
      size = 1;
    }
  }

  boolean isDead() {
    if (lifespan < 0.0) {
      return true;
    } else {
      return false;
    }
  }

  void draw() {
    stroke(0, lifespan);
    fill(#DE8816, lifespan);
    ellipse(x, y, size, size);
  }
}

```

---

<div class="post-metadata">

### Author: ![returng](https://avatars.discourse-cdn.com/v4/letter/r/97f17d/32.png) [@returng](https://discourse.processing.org/u/returng)
#### Post date: [November 16, 2022, 11:04am UTC](https://discourse.processing.org/t/how-can-i-make-the-size-of-ellipse-keeps-the-size-after-i-released-the-mouse/39694/9 "2022-11-16T11:04:27Z")

</div>

Thank you very much!  
I’m sorry for asking too many questions, I wanted to keep the new ones as well when I released for example when I released the mouse the new ball spawn will be smaller than the original so that when I mousePressed again it reset (return all back to normal)

---

<div class="post-metadata">

### Author: ![returng](https://avatars.discourse-cdn.com/v4/letter/r/97f17d/32.png) [@returng](https://discourse.processing.org/u/returng)
#### Post date: [November 16, 2022, 11:05am UTC](https://discourse.processing.org/t/how-can-i-make-the-size-of-ellipse-keeps-the-size-after-i-released-the-mouse/39694/10 "2022-11-16T11:05:17Z")

</div>

Thank you very much!

I’m sorry for asking too many questions, I wanted to keep the new ones as well when I released for example when I released the mouse the new ball spawn will be smaller than the original so that when I mousePressed again it reset (return all back to normal)

---

<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: [November 16, 2022, 11:17am UTC](https://discourse.processing.org/t/how-can-i-make-the-size-of-ellipse-keeps-the-size-after-i-released-the-mouse/39694/11 "2022-11-16T11:17:11Z")

</div>

> [@returng](#):
>
> I wanted to keep the new ones as well when I released

then use a global var `size2` and move(!) this part to draw

```auto
 if (mousePressed ) {
    size2 = size2 - 0.51;
  } 

  if (size2<2.6) {
    size2 = 2.6;
  }

```

in the class say `float size=size2; // Size of the particle` (BEFORE the Constructor)

- and delete this from the Constructor this.size = 20;
