Modify a text file: $/button-practice.pde
$/button-practice.pde
//////// Button objects, etc. Button a, b, c, d, e; String msg= "Last click: "; String lastClick="?"; void setup() { //// Initialize before drawing any frames. size(640, 480); smooth(); a= new Button("A", 10, 100); b= new Button("B", 10, 200, color(127,127,255), 0); c= new Button("C", 10, 300); d= new Button("D", 10, 400, color(127,255,255), 0); d.w= 150; e= new Button("E", 210, 400, color(127,255,127), 1); e.s= "EEE"; } void draw() { //// Display the scene, etc. background( 255, 255, 63 ); text( "Last click was: "+lastClick, 500,10 ); // a.show(); b.show(); if (lastClick == "B") b.h= 75; c.show(); d.show(); if (lastClick == "E") e.w= 150; else e.w=75; e.show(); } void mousePressed() { //// Which button was clicked? lastClick="?"; if (a.isOver(mouseX,mouseY) ) lastClick= "A"; if (b.isOver(mouseX,mouseY) ) lastClick= "B"; if (c.isOver(mouseX,mouseY) ) lastClick= "C"; if (d.isOver(mouseX,mouseY) ) lastClick= "D"; if (e.isOver(mouseX,mouseY) ) lastClick= "E"; } void action( String s ) { //// Display this letter. text (s, 400,400 ); } class Button { //// A button, displayed on screen, and able to detect when mouse is OVER. int x= 10; // Position and size of this button. int y= 10; int w= 100; int h= 50; color c; // Color of this button. int type=1; // Button type (1=rectangle) String s; // Text on this button. //// CONSTRUCTOR(S) //// Button() { //// Default constructor. c=color(255,127,127); s="BUTTON"; } Button( String name) { //Button(); //// Accept x & y coordinates; use default for the others. c=color(255,127,127); s=name; } Button( String name, int xset, int yset) { //Button(); //// Accept x & y coordinates; use default for the others. x= xset; y= yset; c=color(255,127,127); s=name; } Button( String name, int xset, int yset, color cset, int tset) { //Button(); //// Accept x & y coordinates; use default for the others. x= xset; y= yset; c= cset; type= tset; s=name; } //// METHODS() //// void show() { //// Show the button. fill(c); if (type<1) { ellipseMode(CORNER); ellipse(x,y,w,h); }else{ rectMode(CORNER); rect(x,y,w,h); } // Name on button. fill(0); text( s, x+10, y+h/2 ); } boolean isOver( int xx, int yy ) { //// Return true if xx,yy is over this button! if (xx < x) return false; else if (xx > x+w) return false; else if (yy < y) return false; else if (yy > y+h) return false; background(0); return true; //OR: return ( xx>x && xx
y && yy