The output is missing when button is pressed

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.

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

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

There are two errors in this code:

  1. When checking wich buttons are clicked you write the index in menu
if(buttons[i].isClicked()){
menu=i
}

You should write it in mode instead

if(buttons[i].isClicked()){
mode=i
}
  1. When detecting if a button is clicked you use this code
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

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

instead.

I hope I could help you!

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

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

with

if (mousePressed !=true)    Pressed = false;

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