// Button objects // Prof.BAM: h1b_buttons.pde String title= "Button objects", author= "Prof.BAM: h1b_buttons.pde"; Button one, two, three; boolean day=true; color g= color(200, 255, 200); color sky= color(200, 220, 255); void setup() { size( 600, 400 ); one= new Button(); two= new Button( "TWO" ); two.x=200; two.y=200; three= new Button( "3", 300, 300 ); three.c= color( 0,250,0 ); } void draw() { scene(); showAll(); gossip(); } void scene() { if (day) { background( sky ); // day sky } else { background( 120, 150, 180 ); // nite } fill( g ); rect( 0, height/5, width, height*4/5 ); // grass; // } void showAll() { one.show(); two.show(); three.show(); } void gossip() { textSize(24); text( title, 10, 20 ); textSize(12); text( author, 10, height-10 ); } //// EVENT HANDLERS //// void keyPressed() { if (key == 'q') { exit(); } if (key == 'd') { day = ! day; } } void mousePressed() { // Change day/nite if click inside button if ( one.hit() ) { day = ! day; } if ( two.hit() ) { g= color(120,50,50); } if ( three.hit() ) { g= color(0,255,0); } } //// OBJECT CLASSES //// class Button { // PROPERTIES // float x=100, y=100; int w=80, h=30; String name="Click here"; color c=color( 255, 200, 200 ); // CONSTRUCTORS // Button() {} Button( String nn ) { name=nn; } Button( float x2, float y2 ) { x=x2; y=y2; } Button( String nn, float x2, float y2 ) { name=nn; x=x2; y=y2; } // METHODS // void show() { fill( c ); rect( x,y, w,h ); fill(0); textSize(12); text( name, x+10, y+20 ); } boolean hit() { return( mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h ); } boolean hit( float x2, float y2) { if (x2 < x) return false; if (x2 > x+w) return false; if (y2 < y) return false; if (y2 > y+h) return false; return true; } }// END OF class Button. //