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);
}






  • http://www.mimran.com&patrickmimran.com Patrick Mimran

    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

  • http://www.learningprocessing.com Daniel Shiffman

    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.

  • http://www.mimran.com&patrickmimran.com Patrick Mimran

    Thank you very much

    Patrick

  • mhelke

    what if I want to have more than one of these rectangles?
    I tried this but it doesn’t work…// Learning Processing
    // Daniel Shiffman
    // http://www.learningprocessing.com

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

    int x = 800;
    int y = 230;
    int w = 10;
    int h = 10;

    boolean button2 = false;
    int x1 = 200;
    int y1 = 230;
    int w1 = 10;
    int h1 = 10;

    PImage b;
    PImage c;
    PImage d;
    PImage e;

    void setup() {
    size(960,540);
    noStroke();
    }

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

    if (button1) {
    b = loadImage("first.gif");
    background(b);
    noStroke();
    } else {
    c = loadImage("second.gif");
    background(c);

    }

    if (button2) {
    d = loadImage("first.jpeg");
    background(d);
    noStroke();
    } else {
    e = loadImage("second.jpeg");
    background(e);

    }

    fill(#e09554);
    rect(x,y,w,h);
    rect(x1,y1,w1,h1);
    }

  • Anonymous

    your second conditional needs to reference x1, not x. Also, you’ll want to use an if, else if, else structure, i.e.

    if (button1) {
    background(0,255,0);
    } else if (button2) {
    background(255,0,0);
    } else {
    background(0,0,255);
    }

    Object-oriented programming will really make this easier. See:

    http://www.learningprocessing.com/exercises/chapter-9/exercise-9-8/

  • http://profiles.google.com/will.price94 Will Price

    For this bit ” if(button) ”
    why does it not read “if(button == true)”

  • Anonymous

    These are equivalent. if (button) is simple a shorter way of writing it. Since a boolean variable can only be “true” or “false” by definition, asking if it is equal to true is redundant.