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

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);
}

}

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()…

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);
}

}

1 Like

Hmm.

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

Replace 0.5 with 0.01

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

1 Like

Post your entire code please

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);
}

}

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

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


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);
  }
}

1 Like

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)

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)

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

 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;
1 Like