Using buttons for a choice and having 1 button already chosen at start
in
Programming Questions
•
1 year ago
I have created 4 buttons, when you mouse over 1 button it changes color to highlight and when its not over it returns to a background color. If mouse is pressed it change and stays highlighted. If you chose another it highlights and returns the previous button to the background color. The problem Im having is I would like to have 1 button start as highlighted but the first time it hits update in my code it changes back to the background. Im having trouble on the logic part that would keep the button highlighted untill another is pressed
The code is below Thanks for the help Ken
The code is below Thanks for the help Ken
- int wWidth = 600;
int wHeight = 800;
int graphWidth = wWidth-90;
int graphHeight = wHeight/3-35;
color c1 = color(255,0,0);
color c2 = color(0,255,0);
color c3 = color(0,0,255);
RectButton rect1,rect2,rect3,rect4;
//Button stuff
boolean locked = false;
void setup(){
size(wWidth,wHeight);
rect1 = new RectButton("/.5",10,10+0, 25, 25, c1, c2);
rect2 = new RectButton("/1",10,10+25, 25, 25, c1, c2);
rect3 = new RectButton("/2",10,10+50, 25, 25, c1, c2);
rect4 = new RectButton("/4",10,10+75, 25, 25, c1, c2);
}
void draw(){
background(120);
rect1.displayButton();
rect2.displayButton();
rect3.displayButton();
rect4.displayButton();
update(mouseX, mouseY);
}
void update(int x, int y){
if(locked == false) {
rect1.update();
rect2.update();
rect3.update();
rect4.update();
}
else {
locked = false;
}
if(mousePressed) {
if(rect1.pressed()) {
println("HERE1");
rect1.basecolor = c2;
rect2.basecolor = c1;
rect3.basecolor = c1;
rect4.basecolor = c1;
} else if(rect2.pressed()) {
println("HERE2");
rect1.basecolor = c1;
rect2.basecolor = c2;
rect3.basecolor = c1;
rect4.basecolor = c1;
} else if(rect3.pressed()) {
rect1.basecolor = c1;
rect2.basecolor = c1;
rect3.basecolor = c2;
rect4.basecolor = c1;
} else if(rect4.pressed()) {
rect1.basecolor = c1;
rect2.basecolor = c1;
rect3.basecolor = c1;
rect4.basecolor = c2;
}
}
}
class Button
{
String Name;
int x, y;
int sizeX;
int sizeY;
color basecolor, highlightcolor;
color currentcolor;
boolean over = false;
boolean pressed = false;
boolean start = true;
void update()
{
if(over()) { currentcolor = highlightcolor;
println("HERE3");
}
else { currentcolor = basecolor;
}
locked=false;
}
boolean pressed()
{
if(over) {
locked = true;
currentcolor = highlightcolor;
return true;
}
else {
locked = false;
currentcolor = basecolor;
return false;
}
}
boolean over()
{
return true;
}
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 RectButton extends Button
{
RectButton(String A,int ix, int iy, int xsize,int ysize, color icolor, color ihighlight)
{
Name = A;
x = ix;
y = iy;
sizeX = xsize;
sizeY = ysize;
basecolor = icolor;
highlightcolor = ihighlight;
if(A == "/1"){
//locked = false;
currentcolor = highlightcolor;
}else{
currentcolor = basecolor;
}
println(currentcolor);
}
boolean over()
{
if( overRect(x, y, sizeX, sizeY) ) {
over = true;
return true;
}
else {
over = false;
return false;
}
}
void displayButton()
{
stroke(0);
fill(currentcolor);
rect(x, y, sizeX, sizeY);
fill(0);
textAlign(CENTER);
text(Name,x+sizeX/2,y+sizeY/2+5);
}
}
1