Loading...
Processing Forum
Recent Topics
All Forums
Screen name:
sannin
sannin's Profile
1
Posts
1
Responses
0
Followers
Activity Trend
Last 30 days
Last 30 days
Date Interval
From Date :
To Date :
Go
Loading Chart...
Posts
Responses
PM
Show:
All
Discussions
Questions
Expanded view
List view
Private Message
Object based buttons.
[3 Replies]
02-Dec-2011 12:29 AM
Forum:
Programming Questions
I know this might be something simple but its driving me nuts. How do I get each button to do something different? Thanks.
ImageButtons1 button1;
ImageButtons1 button2;
ImageButtons1 button3;
PImage bg;
void setup()
{
size(800,600);
bg = loadImage("lcarsbg.jpg");
PImage b = loadImage("button_orange.gif");
PImage r = loadImage("button_pale_red.gif");
PImage d = loadImage("button_orange.gif");
int x = 225;
int y = 250;
int w = b.width;
int h = b.height;
button1 = new ImageButtons1(x, y, w, h, b, r, d);
PImage bf = loadImage("button_orange.gif");
PImage rf = loadImage("button_pale_red.gif");
PImage df = loadImage("button_orange.gif");
int xf = 225;
int yf = 300;
int wf = bf.width;
int hf = bf.height;
button2 = new ImageButtons1(xf, yf, wf, hf, bf, rf, df);
PImage bh = loadImage("button_orange.gif");
PImage rh = loadImage("button_pale_red.gif");
PImage dh = loadImage("button_orange.gif");
int xh = 225;
int yh = 350;
int wh = bh.width;
int hh = bh.height;
button3 = new ImageButtons1(xh, yh, wh, hh, bh, rh, dh);
}
void draw()
{
background(bg);
button1.update();
button1.display();
button2.update();
button2.display();
button3.update();
button3.display();
}
class Button1
{
int x, y;
int w, h;
color basecolor, highlightcolor;
color currentcolor;
boolean over = false;
boolean pressed = false;
void pressed() {
if(over && mousePressed) {
pressed = true;
} else {
pressed = false;
}
}
boolean overRect(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
}
class ImageButtons1 extends Button1
{
PImage base;
PImage roll;
PImage down;
PImage currentimage;
ImageButtons1(int ix, int iy, int iw, int ih, PImage ibase, PImage iroll, PImage idown)
{
x = ix;
y = iy;
w = iw;
h = ih;
base = ibase;
roll = iroll;
down = idown;
currentimage = base;
}
void update()
{
over();
pressed();
if(pressed) {
currentimage = down;
} else if (over){
currentimage = roll;
} else {
currentimage = base;
}
}
void over()
{
if( overRect(x, y, w, h) ) {
over = true;
} else {
over = false;
}
}
void display()
{
image(currentimage, x, y);
}
}
«Prev
Next »
Moderate user : sannin
Forum