We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Buttons (Read 679 times)
Buttons
Apr 23rd, 2009, 10:59am
 
I'm tryna create 4 image buttons and have the image changed to a check box whenever one of them is clicked. If a different image is clicked, every other button should revert to the original image and only the one that was last clicked should have the image with a check mark on it. It's kinda like radio buttons such that only one can be selected at  a time. How can i implement that?
Re: Buttons
Reply #1 - Apr 23rd, 2009, 11:05am
 
probably just using a simple Boolean value assigned to each, then use an if clause to draw the appropriate thing.  You could also use a switch/case statement.
Re: Buttons
Reply #2 - Apr 23rd, 2009, 12:01pm
 
class Check {
int x, y; // The x- and y-coordinates
int size; // Dimension (width and height)
color baseGray; // Default gray value
boolean checked = false; // True when the check box is selected
Check(int xp, int yp, int s, color b) {
x = xp;
y = yp;
size = s;
baseGray = b;
}
// Updates the boolean variable checked
void press(float mx, float my) {
if ((mx >= x) && (mx <= x+size) && (my >= y) && (my <= y+size)) {
checked = true; // Toggle the check box on and off
}
else
checked=false;
}
// Draws the box and an X inside if the checked variable is true
void display() {
stroke(255);
fill(baseGray);
rect(x, y, size, size);
if (checked == true) {
line(x, y, x+size, y+size);
line(x+size, y, x, y+size);
}
}
}

int numChecks = 4;
Check[] checks = new Check[numChecks];
void setup() {
size(100, 100);
int x = 14;
int y = 14;
for (int i = 0; i < numChecks; i++) {
checks[i] = new Check(x, y, 12, color(0));
x += 15;
if (x > 80) {
x = 14;
y += 15;
}
}
}


void draw() {
background(0);
for (int i=0; i<numChecks; i++) {
checks[i].display();
}
}
void mousePressed() {
for (int i = 0; i < numChecks; i++) {
checks[i].press(mouseX, mouseY);
}
}


from:
Processing:
a programming
handbook for
visual designers
and artists
Casey Reas
Ben Fry
Page Index Toggle Pages: 1