Created
November 6, 2013 13:45
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Button myB1; | |
Button myB2; | |
Button myB3; | |
Button myB4; | |
void setup() | |
{ | |
size(400, 150); | |
background(255); | |
myB1 = new Button(50,50,50,50,color(255,0,0), "Button 1"); | |
myB2 = new Button(150,50,50,50,color(0,255,0), "Button 2"); | |
myB3 = new Button(250,50,50,50,color(0,0,255), "Button 3"); | |
myB4 = new Button(300,150, "twitter_icon.png"); | |
//myB4 = new Button(300,150, loadImage("twitter_icon.png")); | |
} | |
void draw() | |
{ | |
myB1.drawButton(); | |
myB2.drawButton(); | |
myB3.drawButton(); | |
myB4.drawButton(); | |
myB1.isPositionWithinButton(mouseX, mouseY); | |
myB2.isPositionWithinButton(mouseX, mouseY); | |
myB3.isPositionWithinButton(mouseX, mouseY); | |
myB4.isPositionWithinButton(mouseX, mouseY); | |
} | |
/////////////////////////////////////////////////////////////// | |
class Button | |
{ | |
float _x; //position x | |
float _y; //position y | |
float _sX; //sizeX (width?) | |
float _sY; //sizeY (height?) | |
color _c; //color | |
String _t; //text | |
PImage _image = null; //see drawButton | |
//PImage _image; //you don't have to do this because of: PImage _image = null; | |
//this is the constructor, constructors doesn't have a type so they can't be voids | |
Button(float x, float y, float sX, float sY, color c, String t) | |
{ | |
_x = x; | |
_y = y; | |
_sX = sX; | |
_sY = sY; | |
_c = c; | |
_t = t; | |
} | |
// | |
Button(float x, float y, PImage image) | |
{ | |
_x = x; | |
_y = y; | |
_image = loadImage(image); | |
_t = image; | |
_sX = _image.width; | |
_sY = _image.height; | |
} | |
// | |
void drawButton() | |
{ | |
//you do this to avoid crashing the program if the image isn't there | |
if (_image == null) | |
{ | |
fill(_c); | |
//fill(_red,_green,_blue); instead of color _c; | |
text(_t, _x, _y-10); | |
rect(_x, _y, _sX, _sY); | |
} | |
else | |
{ | |
image(_image, _x, _y); | |
} | |
} | |
// | |
boolean isPositionWithinButton(float x, float y) | |
{ | |
if (x > _x && x < _x+_sX && y > _y && y < _y+_sY) | |
{ | |
println(_t); | |
return true; | |
} | |
else | |
return false; | |
} | |
// | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment