I followed this example when making my own buttons: Processing buttons example
I want the colour of the button to change, when the mouse is hovering above it, and the background to change when the first button is pressed. However, instead all buttons are connected, and only change colour when the mouse is hovering above the third of the buttons (but the first button still works).
Here is some of my code (didn’t know what exactly to include)
int buttonSize = 260; // Diameter af alle knapper
int button1X = 185; // X positionen af knappen
int button2X = 495; // X positionen af knappen
int button3X = 805;
int buttonY = 500;
color button1Color, button2Color, button3Color, baseColor;
color button1Highlight;
color button2Highlight;
color button3Highlight;
color current1Color;
boolean button1Over = false;
boolean button2Over = false;
boolean button3Over = false;
void setup() {
size(1250,800);
button1Color = color(255);
button2Color = color(255);
button3Color = color(255);
button1Highlight = color(204);
button2Highlight = color(204);
button3Highlight = color(204);
baseColor = color(#D5ECF9);
current1Color = baseColor;
}
void draw() {
update(mouseX, mouseY);
background(current1Color);
if (button1Over) {
fill(button1Highlight);
} else {
fill(button1Color);
}
if (button2Over) {
fill(button2Highlight);
} else {
fill(button2Color);
}
if (button3Over) {
fill(button3Highlight);
} else {
fill(button3Color);
}
stroke(0);
strokeWeight(stroke);
rect(button1X, buttonY, buttonSize, buttonSize, 35);
rect(button2X, buttonY, buttonSize, buttonSize, 35);
rect(button3X, buttonY, buttonSize, buttonSize, 35);
}
void update(int x, int y) {
if ( overButton1(button1X, buttonY, buttonSize, buttonSize) ) {
button1Over = true;
button2Over = false;
button3Over = false;
}
else if ( overButton2(button2X, buttonY, buttonSize, buttonSize) ) {
button2Over = true;
button1Over = false;
button3Over = false;
}
else if ( overButton3(button3X, buttonY, buttonSize, buttonSize) ) {
button3Over = true;
button1Over = false;
button2Over = false;
}
else {
button1Over = button2Over = button3Over = false;
}
}
void mousePressed() {
if (button1Over) {
current1Color = button1Color;
}
}
boolean overButton1(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
boolean overButton2(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
boolean overButton3(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}