How to change color with keyPressed()

edited January 2016 in Questions about Code

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?

Tagged:

Answers

  • edited January 2016

    On a global level c is just an int, you need to do

    if (key == 'r') {
     for (ExpandingCircle ec: circles) {
      ec.c = color(255, 0, 0);
     }
    }
    

    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

  • edited January 2016

    Sure, learn how to format code, BTW.

    You need to pass color to the object constructor then:

     ExpandingCircle(int x, int y, boolean transparencyOn, color c_) {
         c = c_;
         // all you have there now except c = random(...
        }
    

    And you need to change all lines where you create ExpandingCircle with new keyword, so they be new ExpandingCircle(mouseX, mouseY, true, myColor), you also need that myColor variable declared on top, so if (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.

  • edited January 2016

    Here, if you press r new circles will be red, if b, they will be blue, as they were:

    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();
     }
    }
    
    class ExpandingCircle {
     int x, y;
     float radius;
     color c;
     boolean transparencyOn;
     int transparency;
    
     ExpandingCircle(int x, int y, boolean transparencyOn, int mode) {
      this.x = x;
      this.y = y;
      this.transparencyOn = transparencyOn;
    
      if (mode == 1) {
       c = color(random(155, 181), random(0, 50), 30);
      } else {
       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() {
     if (mouseButton == LEFT) {
      circles.add(new ExpandingCircle(mouseX, mouseY, false, c));
     } else {
      circles.add(new ExpandingCircle(mouseX, mouseY, true, c));
     }
    }
    
    void mousePressed() {
     if (mouseButton == LEFT) {
      circles.add(new ExpandingCircle(mouseX, mouseY, false, c));
     } else {
      circles.add(new ExpandingCircle(mouseX, mouseY, true, c));
     }
    }
    
    void keyPressed() {
     if (key == 'r') {
      c = 1;
     }
     if (key == 'b') {
      c = 0;
     }
    
     if (key == ' ') {
      circles.clear();
     }
     if (key == 's') {
      saveFrame("#########.png");
     }
    }
    
Sign In or Register to comment.