xxxxxxxxxx
// Variables for the button
int buttonx;
int buttony;
int buttonw = 50;
int buttonh = 70;
// checks if button has been clicked or not
boolean bgChange = false;
// Variables for the ellipse
int elX;
int elY;
int elW = 50;
void setup() {
size(300, 300);
buttonx = width/4;
buttony = height/4;
elX = width*3/4;
elY = height*3/4;
}
void draw() {
if (bgChange) {
background(255, 220, 0);
} else {
background(0, 150, 100);
}
if ((mouseX > buttonx) &&
(mouseY < buttony + buttonh) &&
(mouseY > buttony) &&
(mouseX < buttonx + buttonw)) {
fill(23, 50, 100); // dark blue
} else {
fill(#e8b2c5); // light pink
}
rect(buttonx, buttony, buttonw, buttonh);
//distance = sqrt((elX-mouseX)2 + (elY-mouseY)2);
// dist is for circle hit detection, it's hit detection between two points
//- works for circles because we draw ellipse using x,y and radius
// so we can check if (distance (Cx1, Cy1, mouseX, mouseY ) > radius //(when radius is width/2 of circle)
if (dist(mouseX, mouseY, elX, elY) < elW/2) {
fill(#9cd3c9); //sea green
} else {
fill(100, 100, 255); // purplish blue
}
ellipse(elX, elY, elW, elW);
}
void mouseClicked() {
// make square clickable
if ((mouseX > buttonx) &&
(mouseY < buttony + buttonh) &&
(mouseY > buttony) &&
(mouseX < buttonx + buttonw)) {
if (bgChange == true) {
bgChange = false;
} else {
bgChange = true;
//bgChange = !bgChange; //same as above ^ shorter way to say the same.
}
}
// make ellipse clickable
if (dist(elX, elY, mouseX, mouseY) < elW/2) {
bgChange = !bgChange;
}
}