Skip to content

Instantly share code, notes, and snippets.

Created October 29, 2013 09:51
Show Gist options
  • Save anonymous/7211726 to your computer and use it in GitHub Desktop.
Save anonymous/7211726 to your computer and use it in GitHub Desktop.
Button myB1;
Button myB2;
Button myB3;
void setup()
{
size(350, 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");
}
void draw()
{
myB1.drawButton();
myB2.drawButton();
myB3.drawButton();
myB1.isPositionWithinButton(mouseX, mouseY);
myB2.isPositionWithinButton(mouseX, mouseY);
myB3.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; //see drawButton
//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;
//this(x, y, sX, sY, t, null);
_c = c;
_t = t;
}
//
Button(PImage image)
{
_image = image;
// something with image and null
}
//
void drawButton()
{
_image = loadImage("twitter_icon.png"); //"twitter_icon.png"
//you do this to avoid crashing the program if the image isn't there
if (_image == null)
{
fill(color(_c)); //fill(_red,_green,_blue); instead of color _c;
text(_t, _x, _y-10);
rect(_x, _y, _sX, _sY);
}
}
//
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