# Turning randomly placed buttons off and on via boolean switch

**URL:** <https://discourse.processing.org/t/turning-randomly-placed-buttons-off-and-on-via-boolean-switch/16917>\
**Category:** Coding Questions\
**Created:** [January 6, 2020, 9:32pm UTC](https://discourse.processing.org/t/turning-randomly-placed-buttons-off-and-on-via-boolean-switch/16917 "2020-01-06T21:32:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [January 6, 2020, 9:32pm UTC](https://discourse.processing.org/t/turning-randomly-placed-buttons-off-and-on-via-boolean-switch/16917/1 "2020-01-06T21:32:20Z")

</div>

I think this is a question about **program flow and how to enable user interaction.**  
Currently working on a problem in Shiffman book.

- I want to create multiple buttons (currently 2 buttons in this version) via object class iteration.
- When I mouse click within a button area, button color changes.

I am not seeing the desired on / off interaction via boolean switch.  
Code below:  
////////////////////////////////////////////////

```auto
Button b1;
Button b2;

void setup() {
  size (600, 600);
  b1 = new Button(random(width), random(height), random(50, 100));
  b2 = new Button(random(width), random(height), random(50, 100));
}

void draw() {

  b1.display();
  b2.display();

  b1.click();
  b2.click();
}
void mousePressed() {

  if (mouseX > x && mouseX < x + sz && mouseY > y && mouseY < y + sz) {
    on = !on;
  }
}

```

CLASS//////////////////////////////////////////////////

```auto
class Button {
  float x;
  float y;
  float sz;
  boolean on;

  Button(float tempX, float tempY, float tempSz) {
    x = tempX;
    y = tempY;
    sz = tempSz;

    on = false;
  }

  void display() {
    rect (x, y, sz, sz);
  }

  void click() {

    if (on) {
      fill (255);
      stroke(0);
    } else {
      fill (0);
      stroke (255);
    }

    if (mouseX > x && mouseX < x + sz && mouseY > y && mouseY < y + sz) {
      on = !on;
    }
  }
}

```

Any guidance most welcomed!! 🙂 🙂

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 6, 2020, 10:39pm UTC](https://discourse.processing.org/t/turning-randomly-placed-buttons-off-and-on-via-boolean-switch/16917/2 "2020-01-06T22:39:46Z")

</div>

```auto

Button b1;
Button b2;

void setup() {
  size (600, 600);
  b1 = new Button(random(width), random(height), random(50, 100));
  b2 = new Button(random(width), random(height), random(50, 100));
}

void draw() {
  b1.display();
  b2.display();
}

// -----------------------------------------------------------------------------------

void mousePressed() {
  b1.click();
  b2.click();
}

//============================================================================
// CLASS//////////////////////////////////////////////////

class Button {
  float x; // pos 
  float y;
  float sz; // size 
  boolean on;

  Button(float tempX, float tempY, 
    float tempSz) {
    x = tempX;
    y = tempY;
    sz = tempSz;

    // check against right and lower screen border 
    if (x+sz>=width)
      x=width-sz-1; 

    if (y+sz>=height)
      y=height-sz-1; 

    on = false;
  }

  void display() {

    // eval and use on HERE 
    if (on) {
      fill (255);
      stroke(0);
    } else {
      fill (0);
      stroke (255);
    }

    rect (x, y, sz, sz);
  }

  void click() {
    if (mouseX > x &&
      mouseX < x + sz && 
      mouseY > y && 
      mouseY < y + sz) {
      on = !on;
    }
  }
  //
}
//

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 6, 2020, 10:40pm UTC](https://discourse.processing.org/t/turning-randomly-placed-buttons-off-and-on-via-boolean-switch/16917/3 "2020-01-06T22:40:54Z")

</div>

> [@debxyz](#):
>
> void mousePressed() {  
> if (mouseX \> x && mouseX \< x + sz && mouseY \> y && mouseY \< y + sz) {  
> on = !on;  
> }  
> }

that was the main problem.

It belongs in the class.

I now call click from mousePressed, instead of from draw

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [January 6, 2020, 11:03pm UTC](https://discourse.processing.org/t/turning-randomly-placed-buttons-off-and-on-via-boolean-switch/16917/4 "2020-01-06T23:03:14Z")

</div>

Ah!! That makes sense!  
Thank you @Chrisir  
😀
