I need help with the color change of the dots in the image I created

I need help with the color change of the dots in the image I created

I added a colorwheel, I create a point with mousepress and drop it to the ground. The color I choose from Colorwheel changes the color of the dots on the whole screen. However, the falling point should stay in the color I chose first, and its color should not change. After changing the color from ColorWheel the new dots should be in the new color.

Note: I do not know the rules such as asking questions and opening topics very well. I’m sorry in advance if I’m missing something.

 import controlP5.*;
ControlP5 gui;

color renk;

int liquidity = 10;
int r = 2;

ArrayList <Particle> all = new ArrayList<Particle>();
float[] h;


void setup() 
{
  size(1000, 600);

  gui = new ControlP5(this);
  h = new float[width];
  noSmooth();
  frameRate(60);
  background(255);
  //fill(255);

  gui.addColorWheel("renk", 840, 10, 120)
    .setRGB(color(0, 45, 90));
}

void keyPressed() 
{
  for (int i = 0; i<width; i++) 
  {
    h[i] = 0;
  }
}

void draw() 
{
  rect(-1, -1, width, height);
  if (mouseY<height-h[constrain(mouseX, 0, width-1)]&&mousePressed) 
  {
    for (int i = 0; i<40; i++) 
    {
      all.add(new Particle(mouseX, mouseY));
      stroke(renk);
    }
  }
  for (int i = 0; i<all.size(); i++) 
  {
    all.get(i).show();
  }
  for (int i = 0; i<all.size(); i++) 
  {
    if (all.get(i).dead)
    {
      all.remove(i);
      i--;
    }
  }
  for (int i = 0; i<width; i++) //düşen noktaların sadece düştüğü yerde kalması düşerken oluşturduklarının silinmesi için

  {
    line(i, height, i, height-h[i]);
  }
  for (int n = 0; n<liquidity; n++) //
  {
    for (int i = 1; i<width; i++) 
    {
      if (abs(h[i]-h[i-1])>3) 
      {
        float avg = (h[i]+h[i-1])/2;
        h[i] = avg;
        h[i-1] = avg;
      }
    }
  }
}

class Particle 
{

  boolean dead = false;
  float x, y, vx, vy;
  float px, py;
  Particle(int xp, int yp) 
  {
    x = xp;
    y = yp;
    px = x;
    py = y;
    float d = random(TWO_PI);
    float l = random(r/PI, r);
    vx = cos(d)*l;
    vy = sin(d)*l;
  } 

  void show() 
  {
    //set((int)x, (int)y, color(renk));
    line(x, y, px, py);
    px = x;
    py = y;
    x+=vx;
    y+=vy;
    vy+=0.1;
    if (x!=constrain(x, 0, width-1)) 
    {
      dead = true;
    } else 
    {
      if (y>height-h[(int)x]) 
      {
        h[(int)x]+=1;
        dead = true;
      }
    }
  }
}
1 Like

you can edit your post

select the code section with the mouse and click the </> in the small command bar

then save

1 Like

Hello,

Welcome to the community.

Links to guidelines:

Please format your code when posting:
https://discourse.processing.org/faq#format-your-code

:)

1 Like

Thank you,

Now, it looks good

1 Like

thanks,
I will read the links you shared when I get the chance, for now I have taken care of the code image.

1 Like

Hello,

Add a color variable for each particle to your particle class.
Apply the color in show() in the class.

In draw():

color col = renk;
all.add(new Particle(mouseX, mouseY, col));

Nice formatting!

:)

1 Like

Hi Glv,

Thank you for the answer. I tried the code as you stated but unfortunately I couldn’t run the code. What am I doing wrong?

import controlP5.*;
ControlP5 gui;

color renk;

int liquidity = 10;
int r = 2;

ArrayList <Particle> all = new ArrayList<Particle>();
float[] h;
  
void setup() 
{
  size(1000, 600);

  gui = new ControlP5(this);
  h = new float[width];
  noSmooth();
  frameRate(60);
  background(255);
  //fill(255);

  gui.addColorWheel("renk", 840, 10, 120)
    .setRGB(color(0, 45, 90));
   
  stroke(renk);
}

void keyPressed() 
{
  for (int i = 0; i<width; i++) 
  {
    h[i] = 0;
    //color(renk);
  }
}

void draw() 
{



  rect(-1, -1, width, height);
  if (mouseY<height-h[constrain(mouseX, 0, width-1)]&&mousePressed) 
  {
    for (int i = 0; i<40; i++) 
    {
      all.add(new Particle(mouseX, mouseY));
      stroke(renk);
    }
  }
  for (int i = 0; i<all.size(); i++) 
  {
    all.get(i).show();
    stroke(renk);
  }
  for (int i = 0; i<all.size(); i++) 
  {
    if (all.get(i).dead)
    {
      all.remove(i);
      i--;
    }
  }
  for (int i = 0; i<width; i++) 

  {
    line(i, height, i, height-h[i]);
  }
  for (int n = 0; n<liquidity; n++) 
  {
    for (int i = 1; i<width; i++) 
    {
      if (abs(h[i]-h[i-1])>3) 
      {
        float avg = (h[i]+h[i-1])/2;
        h[i] = avg;
        h[i-1] = avg;
      }
    }
  }
}

class Particle 
{
 
  boolean dead = false;
  float x, y, vx, vy;
  float px, py;
  Particle(int xp, int yp) 
  {
    color col = renk;
    x = xp;
    y = yp;
    px = x;
    py = y;
    float d = random(TWO_PI);
    float l = random(r/PI, r);
    vx = cos(d)*l;
    vy = sin(d)*l;
  } 

  void show() 
  {
    //set((int)x, (int)y, color(renk));
    line(x, y, px, py);
    px = x;
    py = y;
    x+=vx;
    y+=vy;
    vy+=0.1;
   
       if (x!=constrain(x, 0, width-1)) 
    {
      dead = true;
    } else 
    {
      if (y>height-h[(int)x]) 
      {
        h[(int)x]+=1;
        dead = true;
      }
    }
  }
}

Regards,

Keep trying…

You may have to learn about object- oriented programming first to understand what to do.

I provided enough of a hint.

References:

:)

i keep going and i will find :wink:

Take a look at examples here:
The Nature of Code

They are adding lifespan to a particle.
You want to add a color variable and apply color to your particle.

There are resources (tutorials, references, examples) here:
processing.org

One reference is color:
color() \ Language (API) \ Processing 3+

:)

1 Like