Complex variables

today now the mentioned next ( actually 2 ) step:
variables and functions

// -a- using additional variables for button position 
// -b- using a function for the button and telling it / handing down / the button variables
// -c- and one function for mouse over

// please see how little change needed now, to make one more button  (2)

//_____________________________________________________________ button variables: (1)
int x1 = 100, y1 = 100, w1 =80, h1 =30;
boolean sel1 = false;
String text1 = "text1";

int x2 = x1, y2 = y1+h1+10, w2 =w1, h2 =h1; //_________________ button variables: (2)
boolean sel2 = false; //_______________________________________ (2)
String text2 = "text2"; //_____________________________________ (2)


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

void draw() {
  background(200, 200, 0);
  myButton(x1, y1, w1, h1, sel1, text1);
  myButton(x2, y2, w2, h2, sel2, text2); //____________________ button call: (2)
}

void mousePressed() {
  if ( over(x1, y1, w1, h1) ) sel1 = ! sel1;            // button 1 action
  if ( over(x2, y2, w2, h2) ) sel2 = ! sel2;            // button 2 action: (2)
}


void myButton(int x, int y, int w, int h, boolean sel, String atext) {
  if ( sel )               fill(0, 200, 0);
  else                     fill(0, 0, 200);
  strokeWeight(3);
  if ( over(x, y, w, h) )  stroke(200, 0, 200);
  else                     stroke(0, 200, 200);
  rect(x, y, w, h);
  noStroke();
  fill(200);
  text(atext, x+10, y+h-10);
}

boolean over(int x, int y, int w, int h) {
  return ( mouseX > x & mouseX < x + w &  mouseY > y & mouseY < y + h ) ;
}

if you understand this, all your future ( mouse operated ) projects
will have / use that

over(rx,ry,rw,rh);

function ( inside or outside a class ), and not

if (mouseX > 100 & mouseX < 180 & mouseY > 100 & mouseY < 130) …

4 Likes