Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 5-4: Hold down the button
boolean button = false;

int x = 50;
int y = 50;
int w = 100;
int h = 75;

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

void draw() {
  // The button is pressed if (mouseX,mouseY) is inside the rectangle and mousePressed is true.
  if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h && mousePressed) {
    button = true; 
  } else {
    button = false;
  }

 if (button) {
    background(255);
    stroke(0);
  } else {
    background(0);
    stroke(255);
  }
  
  fill(175);
  rect(x,y,w,h);
}






3 Comments

»

  1. i would like to understand something please , if a boolean is anyway only t or false why do we have to specify
    true or false when we declare the variable name in this example .
    Or if flase is specified when variable is declared why shouldn’t we write only “button” instead of button = false when button false ?

    Thank you

    Patrick

    Comment by Patrick Mimran — March 2, 2010 @ 7:58 am

  2. Yes, the default value of a boolean variable is false. So

    boolean button = false;

    and

    boolean button;

    are the same. However, it’s a good habit to always initialize your variables, just so that your code is more clear.

    Comment by Daniel Shiffman — March 2, 2010 @ 2:07 pm

  3. Thank you very much

    Patrick

    Comment by Patrick Mimran — March 2, 2010 @ 4:54 pm

Leave a comment