# The output is missing when button is pressed

**URL:** <https://discourse.processing.org/t/the-output-is-missing-when-button-is-pressed/36917>\
**Category:** Coding Questions\
**Created:** [May 17, 2022, 1:10pm UTC](https://discourse.processing.org/t/the-output-is-missing-when-button-is-pressed/36917 "2022-05-17T13:10:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjjjjjj](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@jjjjjjj](https://discourse.processing.org/u/jjjjjjj)\
**Post date:** [May 17, 2022, 1:10pm UTC](https://discourse.processing.org/t/the-output-is-missing-when-button-is-pressed/36917/1 "2022-05-17T13:10:47Z")

</div>

Hi, I’m doing an assignment which I have to create a game and I’ve attempted to create a functioning menu. I coded buttons that navigate to different modes(?) when clicked using class (to create button) and switch (to go to different modes) and when I click the button, it does change to the mode specified but doesn’t show the output in the specific mode, e.g. clicking on button 1 doesn’t show the contents of mode1()[shown in code below]. I’m not sure why this is happening. I’d be immensely grateful if someone could help me understand and resolve this issue as soon as possible. Thank you!

Here’s my code.

```auto
int mode=0;
Button[] buttons;

void setup()
{
  size(1400, 780);
  textAlign(CENTER);
  strokeWeight(4);
  buttons = new Button[5];
  buttons[0] = new Button(150, 650, "Back", 250, 252, 251);
  buttons[1] = new Button(150, 300, "1", 229, 235, 215);
  buttons[2] = new Button(150, 400, "2", 247, 215, 215);
  buttons[3] = new Button(150, 500, "3", 174, 188, 196);
  buttons[4] = new Button(150, 600, "4", 212, 238, 227);
}

void draw()
{
  background(226);

  switch(mode)
  {
  case 0:
    {
      fill(0);
      text("Game", 800, 200);
      for (int i=1; i<=4; i++) {
        buttons[i].display();
        buttons[i].update();
        if (buttons[i].isClicked()) {
          menu=i;
        }
      }
    }
    break;
  case 1:
    {
      mode1();
    }
    break;
  case 2:
    {
      mode2();
    }
    break;
  case 3:
    {
      mode3();
    }
    break;
  case 4:
    {
      mode4();
    }
    break;
  case 5:
    {
      complete();
    }
  }
}

void back_menu()
{
  buttons[0].display();
  buttons[0].update();
  if (buttons[0].isClicked()) {
    menu=0;
  }
}

```

Here’s the class I made for the buttons

```auto
class Button {
  int x, y;
  int w = 150;
  int h = 80;
  String t;
  color c;
  boolean over(int x, int y, int w, int h) {
    return (mouseX > x & mouseX < x + w &&
      mouseY > y & mouseY < y + h) ;
  }
  boolean Pressed = false;
  boolean Clicked = false;

  Button (int Bx, int By, String text, int r, int g, int b) {
    x = Bx;
    y = By;
    t = text;
    c = color(r, g, b);
  }

  void update() {
    if (mousePressed == true && mouseButton == LEFT && Pressed == false)
    {
      Pressed = true;
      if (mouseX>=x && mouseX <=x+w && mouseY <=y+h)
      {
        Clicked = true;
      }
    } else
    {
      Clicked = false;
    }
    if (mousePressed !=true)
    {
      Pressed = false;
    }
  }

  void display() {
    if (over(x, y, w, h)) {
      stroke(192, 41, 66);
      fill(c);
      rect(x, y, 150, 80);
      pushStyle();
      fill(0);
      textSize(20);
      text(t, x+w/2, y+h/2);
      popStyle();
    } else {
      stroke(0);
      fill(c);
      rect(x, y, 150, 80);
      pushStyle();
      fill(0);
      textSize(20);
      text(t, x+w/2, y+h/2);
      popStyle();
    }
  }
  boolean isClicked()
  {
    return Clicked;
  }
}

```

---

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [May 17, 2022, 4:16pm UTC](https://discourse.processing.org/t/the-output-is-missing-when-button-is-pressed/36917/2 "2022-05-17T16:16:01Z")

</div>

There are two errors in this code:

1. When checking wich buttons are clicked you write the index in menu

```auto
if(buttons[i].isClicked()){
menu=i
}

```

You should write it in mode instead

```auto
if(buttons[i].isClicked()){
mode=i
}

```

1. When detecting if a button is clicked you use this code

```auto
if (mouseX>=x && mouseX <=x+w && mouseY <=y+h)
      {
        Clicked = true;
      }

```

You however leave out the possibility of clicking above the button. This can be fixed if you use

```auto
if (mouseX>=x && mouseX <=x+w && mouseY <=y+h&& mouseY >=y)
      {
        Clicked = true;
      }

```

instead.

I hope I could help you!

---

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [May 17, 2022, 4:23pm UTC](https://discourse.processing.org/t/the-output-is-missing-when-button-is-pressed/36917/3 "2022-05-17T16:23:36Z")

</div>

Another thing that might be interesting for you is  
that you can leave out the { / } in an if / for / else /while statement if you only have exactly one command written there. One example would be replacing

```auto
if (mousePressed !=true)
    {
      Pressed = false;
    }

```

with

```auto
if (mousePressed !=true) Pressed = false;

```

you also don’t need the { / } with the case part of a switch/case statement.
