Coloring Circles question
in
Programming Questions
•
11 months ago
Hi, I am working on a sketch with moving circles.
Second .pde file to be placed in the same folder of the first one.
I want this circles to appear when I click on the screen with a random color.
Now the random color is assign random each time that Processing draw a new frame.
so I have the circles that change color every frame, (line 23 & 24 of the Code)
any Idea on how to have a random color assigned to the circle at the first frame and no change through the animation?
Thanks
- ArrayList circs;
- void setup()
- {
- size(400, 400);
- background (0);
- fill(255);
- smooth();
- circs = new ArrayList();
- }
- void draw()
- {
- background(0);
- UpdateMovement();
- AdjustForBoundaryCollision();
- AdjustForObjectCollision();
- for (int i=0; i<circs.size(); i++)
- {
- color a = color(random(255),random(255),random(255));
- fill(a);
- Circle c = (Circle)circs.get(i);
- c.draw();
- }
- }
- void AdjustForBoundaryCollision()
- {
- for (int i =0; i<circs.size(); i++)
- {
- Circle c = (Circle)circs.get(i);
- if (c.p.x+(c.r/2) > width ||c.p.x-(c.r/2)<0)
- {
- c.v.x *=-1;
- }
- if (c.p.y+(c.r/2) > height ||c.p.y-(c.r/2)<0)
- {
- c.v.y *=-1;
- }
- }
- }
- void UpdateMovement()
- {
- for (int i=0; i<circs.size(); i++)
- {
- Circle c = (Circle)circs.get(i);
- c.UpdateMovement();
- }
- }
- void AdjustForObjectCollision()
- {
- for (int i=0; i<circs.size(); i++)
- {
- Circle c =(Circle)circs.get(i);
- for (int j=i+1; j<circs.size(); j++)
- {
- Circle c2 = (Circle)circs.get(j);
- PVector cVect = new PVector(0, 0);
- cVect.add(c.p);
- cVect.sub(c2.p);
- if (cVect.mag()<c.r+c2.r)
- {
- c.v.add(cVect);
- c2.v.sub(cVect);
- }
- }
- }
- }
- void mousePressed()
- {
- Circle c = new Circle(mouseX, mouseY);
- circs.add(c);
- }
- void keyPressed()
- {
- save ("CircleCollection.png");
- }
- class Circle
- {
- PVector p;
- PVector v;
- float damper = random(0.9,.9999999999999999999);
- float r;
- //constructor
- Circle(float tx, float ty, float tr)
- {
- p = new PVector(tx, ty);
- v = new PVector(random(-1,1), random(-1,1));
- r = tr;
- }
- Circle(float tx, float ty)
- {
- p = new PVector(tx, ty);
- v = new PVector(random(-1,1), random(-1,1));
- r = random(50);
- }
- void draw()
- {
- ellipse (p.x, p.y, r, r);
- }
- void UpdateMovement()
- {
- //p.x +=v.x;
- // p.y +=v.y;
- p.add(v);
- v.mult(damper);
- }
- }
1