We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, I have a newbie question. I have this code:
////////////////////
ArrayList circles = new ArrayList(); int filled; int c; int transparency;
void setup() { size(500,500); smooth(); }
void draw() { background(255); for (int i=0; i<circles.size(); i++) { ExpandingCircle ec = (ExpandingCircle) circles.get(i); ec.update(); ec.display(); //if (ec.transparency <= 0) { circles.remove(i); } // remove invisible circles } }
class ExpandingCircle { int x,y; float radius; color c; boolean transparencyOn; int transparency;
ExpandingCircle(int x, int y, boolean transparencyOn) { this.x = x; this.y = y; this.transparencyOn = transparencyOn; c = color(random(0,50), random(155,181), 218); transparency = 100; }
void update() { radius++; //transparency --; if (transparencyOn && radius >= 50 && transparency > 0) { transparency--; } if(radius > 200) {transparency --;} }
void display() { //noFill(); //stroke(c, transparency); //strokeWeight(3); fill(c,transparency); ellipse(x,y,radius,radius); noStroke(); } }
void mouseDragged() {
filled = (int) random(0,5); if (filled < 2){noFill(); } else{fill(c,transparency);} if (mouseButton == LEFT) { circles.add(new ExpandingCircle(mouseX,mouseY,false)); } else { circles.add(new ExpandingCircle(mouseX,mouseY,true)); } }
void mousePressed() {
filled = (int) random(0,5); if (filled < 2){noFill(); } else{fill(c,transparency);} if (mouseButton == LEFT) { circles.add(new ExpandingCircle(mouseX,mouseY,false)); } else { circles.add(new ExpandingCircle(mouseX,mouseY,true)); } }
void keyPressed() { if (key == ' ') { circles.clear(); } if (key == 's') {saveFrame("#########.png"); } }
////////////////////////////////
Which draws expanding circles when the mouse is dragged or clicked. I'd like the color of the circles to change to a red shade when I press the key R. I tried to add if(key =='r'){ c = color(255, 0, 0)} but it didn't change anything. Anyone can help? Please?
Answers
On a global level c is just an int, you need to do
this will change color of already created circles, if this is what you asked
No, not exactly. Sorry, I should have specified. The circles that are already drawn mustn't change color. After pressing the R key, the new circles should be painted red. Could you help me, please?
Thank you for your previous answer
Sure, learn how to format code, BTW.
You need to pass color to the object constructor then:
And you need to change all lines where you create ExpandingCircle with
new
keyword, so they benew ExpandingCircle(mouseX, mouseY, true, myColor)
, you also need that myColor variable declared on top, soif (key == 'r') {myColor = color(255,0,0);}
This solution will however break the color randomness that you have now in constructor. This can be solved as well, but first get use to what I've suggested.
Here, if you press r new circles will be red, if b, they will be blue, as they were: