How do you Change color of a rectangle after releasing the mouse click?

Hello, Im trying to make a small code in which when I draw a rectangle it initially is blue but what I want it to do is once I release the mouse it automatically changes to red. This is my code:

int x,y,x2,y2 = 0;

void setup() {
  size(300, 300);
}


void draw() {
  background(255);
  fill(0,0,255);
  rect(x, y, x2, y2);
}

 void mousePressed() {
  x = mouseX;
  y = mouseY;
}


void mouseDragged() {
  x2 = mouseX - x;
  y2 = mouseY - y;
  rect(x, y, x2, y2);
}
void mouseReleased() {
  fill(255,0,0);
  rect(x,y,x2,y2);

}

Answers

  • //before setup
    color c = color(0,0,255);
    
    //instead of line #10
    fill(c);
    
    void mouseReleased(){
      c = color(255, 0, 0);
    }
    
  • Thanks! I forgot to mention that when I draw another rectangle it does the same thing. So when I draw another rectangle it will be blue then red once i release

  • Answer ✓

    either create an ArrayList of colors or a Rectangle class and store the color there

  • I forgot to mention that when I draw another rectangle it does the same thing. So when I draw another rectangle it will be blue then red once i release

    So that means you want to keep the previous rect you drew? If you store all your rectangles in an arrays, you can display all rectangles that are done with a blue fill (with a for loop) and in mouseDragged you draw the one you are currently creating with a red fill.

  • No I want it to disappear when I draw another one

  • edited May 2017
    void mousePressed() {
      background(255);
    }
    

    That needs to be in the code somewhere. It draws over the rectangle.

  • Answer ✓

    Check this.

    Kf

    int x, y, x2, y2 = 0;
    color c = color(0, 0, 255);
    boolean blueMode=false;
    
    void setup() {
      size(300, 300);
    }
    
    
    void draw() {
      background(255);
      fill(c);
      rect(x, y, x2, y2);
    }
    
    void mousePressed() {
    
      x = mouseX;
      y = mouseY;
      setRectColor();
    }
    
    
    void mouseDragged() {
      x2 = mouseX - x;
      y2 = mouseY - y;
      // /// //               rect(x, y, x2, y2);
    }
    void mouseReleased() {
      setRectColor();
    }
    
    void setRectColor() {
      background(255);
    
      blueMode=!blueMode;    //Toggle curent clueMode state
    
      if (blueMode==true)
        c=color(0, 0, 255);
      ///////////////////rect(x,y,x2,y2);
      else
        c=color(255, 0, 0);
    }
    
  • Thanks!

    void mouseReleased() {
      setRectColor();
    }
    
    void setRectColor() {
      background(255);
    
      blueMode=!blueMode;    //Toggle curent clueMode state
    
      if (blueMode==true)
        c=color(0, 0, 255);
      ///////////////////rect(x,y,x2,y2);
      else
        c=color(255, 0, 0);
    }
    

    This is confusing to me however because im kind of new to programming in general but ill learn along the way.

    I eventually figured it out this way: int x, y, x2, y2 = 0;

        color c;
    
        void setup() {
          size(300, 300);
        }
    
    
        void draw() {
          background(255);
          fill(c); 
          rect(x, y, x2, y2);
        }
    
        void mousePressed() {
          x = mouseX;
          y = mouseY;
          c = color(0, 0, 255);
        }
    
    
        void mouseDragged() {
          x2 = mouseX - x;
          y2 = mouseY - y;
    
          rect(x, y, x2, y2);
        }
        void mouseReleased() {
    
          c = color(255, 0, 0);
        }
    

    is there any benefit of doing it either way?

  • Nop... both works as a demo.

    Kf

Sign In or Register to comment.