Android_101: ButtonClass_single

/*
  Creates button from a generic Button class.
  Syntax: name = new Button( x, y, w, h, "title", btnColor, txtColor); 
*/

// Button names
 Button _btn1;
 Button _btn2;
 Button _label;
 Button _quit;
 
 color BLUE = color(64,124,188);
 color LTGRAY = color(185,180,180);
 color YELLOW = color(245,250,13);
 color RED = color(255,0,0);
 color BLACK = color(0,0,0);
 color WHITE = color(255,255,255);

class Button {
 float x, y, w, h;
 String title;
 color btnColor;
 color txtColor;
 
 // Constructor
 Button(int xpos, int ypos, float wt, float ht, String titleStr, color background, color foreground) {
   x = xpos;
   y = ypos;
   w = wt;
   h = ht;
   title = titleStr;
   btnColor = background;
   txtColor = foreground;
 }
 
 void display(){
   fill(btnColor); // button color
   noStroke();
   rect( x, y, w, h, 15); // rounded rect
   fill(txtColor); // text color
   textSize(42);
   text(title, x + 15, y + 15, w, h);
 }
}

void setup() {
  fullScreen();
  orientation(LANDSCAPE);
  background(BLUE);
  _btn1 = new Button( 300, 180, 200, 60, "Button 1", YELLOW, BLACK);
  _btn2 = new Button( 300, 300, 200, 60, "Button 2", RED, BLACK);
  _label = new Button( 300, 410, 300, 60, "Enter data:", BLUE, WHITE); // No mousePressed()
  _quit = new Button( width - 300, 60, 120, 60, "Quit", LTGRAY, BLACK);
}

void draw() {
  _btn1.display();
  _btn2.display();
  _label.display();
  _quit.display();
}

void mousePressed(){
 if((mouseX >= _btn1.x) && (mouseX <= _btn1.x + _btn1.w) && (mouseY >= _btn1.y) && (mouseY <= _btn1.y + _btn1.h)){
  println("You hit _btn1.");
 }
 
 if((mouseX >= _btn2.x) && (mouseX <= _btn2.x + _btn2.w) && (mouseY >= _btn2.y) && (mouseY <= _btn2.y + _btn2.h)){
  println("You hit _btn2.");
 }
 
  if((mouseX >= _quit.x) && (mouseX <= _quit.x + _quit.w) && (mouseY >= _quit.y) && (mouseY <= _quit.y + _quit.h)){
  exit();
 }
}