// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com
// Example 5-3: Rollovers
void setup() {
size(200,200);
}
void draw() {
background(255);
stroke(0);
line(100,0,100,200);
line(0,100,200,100);
// Fill a black color
noStroke();
fill(0);
// Depending on the mouse location, a different rectangle is displayed.
if (mouseX < 100 && mouseY < 100) {
rect(0,0,100,100);
} else if (mouseX > 100 && mouseY < 100) {
rect(100,0,100,100);
} else if (mouseX < 100 && mouseY > 100) {
rect(0,100,100,100);
} else if (mouseX > 100 && mouseY > 100) {
rect(100,100,100,100);
}
}
After a 20 year break from programming this excercise really grabbed my interest. After doing it as above I tried nested if statements (If mouse on left side of screen then if mouse at top of screen etc.) This didn’t make it any prettier.
Then I remembered you could do this
x=(width/2)*int(mouseX>width/2);
y=(height/2)*int(mouseY > height/2);
rect(x,y,width/2,height/2);
What fun!
Comment by Kit — July 27, 2009 @ 6:07 am