Multiple buttons
in
Programming Questions
•
1 year ago
Ok I rewrote my button program. It shows 4 buttons each having a starting color red, then I change one in the setup to start it as the chosen button green, when you hover over the others they highlight to green and then when pressed change all to red except for the one pressed. The problem is that when I click out side the buttons they all go red. I know where the problem is in the mousepressed loop, but cant figure out another way to do the same thing, any Ideas
Thanks Ken
int bNumber = 4;
RectButton [] buttons = new RectButton[bNumber];
color[] colors = {#FF0000,#00FF00};
boolean locked = false;
void setup(){
//frameRate(1);
size(200,200);
for(int i=0; i<bNumber; i++){
buttons[i] = new RectButton( "t",20,20+i*25,25,25,colors[0],colors[1]);
}
buttons[0].setName("/.5");
buttons[1].setName("/1");
buttons[2].setName("/2");
buttons[3].setName("/4");
buttons[0].regColor = colors[1];
}
void draw(){
background(120);
update(mouseX, mouseY);
for(int i=0; i<bNumber; i++){
buttons[i].display();
}
}
void update(int x, int y){
if(locked == false) {
for(int i=0; i<bNumber; i++){
buttons[i].update();
}
}
else {
locked = false;
}
if(mousePressed) {
for(int i=0; i<bNumber; i++){
if(buttons[i].pressed()) {
buttons[i].regColor = colors[1];
}else {
buttons[i].regColor = colors[0];
}
}
}
}
class Button
{
String Name;
int posX, posY;
int sizeX;
int sizeY;
color buttonColor,regColor,highlightColor;
boolean over = false;
boolean pressed = true;
boolean toggle = false;
void setName(String Name){this.Name = Name;}
//void setbuttonColor(color change){this.buttonColor = change;locked=true;}
void update()
{
if(over()) { buttonColor = highlightColor;
}
else { buttonColor = regColor;
}
}
boolean pressed()
{
if(over) {
locked = true;
regColor = highlightColor;
return true;
}
else {
locked = false;
regColor = regColor;
return false;
}
}
boolean over()
{
return true;
}
}
class RectButton extends Button
{
RectButton(String Name,int posX, int posY, int sizeX, int sizeY, color regColor, color highlightColor)
{
this.Name = Name;
this.posX = posX;
this.posY = posY;
this.sizeX = sizeX;
this.sizeY = sizeY;
this.regColor = regColor;
this.highlightColor = highlightColor;
this.buttonColor = regColor;
}
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;
}
}
boolean over()
{
if( overRect(posX, posY, sizeX, sizeY) ) {
over = true;
return true;
}
else {
over = false;
return false;
}
}
void display()
{
stroke(0);
fill(buttonColor);
rect(posX, posY, sizeX, sizeY);
fill(0);
textAlign(CENTER);
text(Name,posX+sizeX/2,posY+sizeY/2+5);
}
}
Thanks Ken
int bNumber = 4;
RectButton [] buttons = new RectButton[bNumber];
color[] colors = {#FF0000,#00FF00};
boolean locked = false;
void setup(){
//frameRate(1);
size(200,200);
for(int i=0; i<bNumber; i++){
buttons[i] = new RectButton( "t",20,20+i*25,25,25,colors[0],colors[1]);
}
buttons[0].setName("/.5");
buttons[1].setName("/1");
buttons[2].setName("/2");
buttons[3].setName("/4");
buttons[0].regColor = colors[1];
}
void draw(){
background(120);
update(mouseX, mouseY);
for(int i=0; i<bNumber; i++){
buttons[i].display();
}
}
void update(int x, int y){
if(locked == false) {
for(int i=0; i<bNumber; i++){
buttons[i].update();
}
}
else {
locked = false;
}
if(mousePressed) {
for(int i=0; i<bNumber; i++){
if(buttons[i].pressed()) {
buttons[i].regColor = colors[1];
}else {
buttons[i].regColor = colors[0];
}
}
}
}
class Button
{
String Name;
int posX, posY;
int sizeX;
int sizeY;
color buttonColor,regColor,highlightColor;
boolean over = false;
boolean pressed = true;
boolean toggle = false;
void setName(String Name){this.Name = Name;}
//void setbuttonColor(color change){this.buttonColor = change;locked=true;}
void update()
{
if(over()) { buttonColor = highlightColor;
}
else { buttonColor = regColor;
}
}
boolean pressed()
{
if(over) {
locked = true;
regColor = highlightColor;
return true;
}
else {
locked = false;
regColor = regColor;
return false;
}
}
boolean over()
{
return true;
}
}
class RectButton extends Button
{
RectButton(String Name,int posX, int posY, int sizeX, int sizeY, color regColor, color highlightColor)
{
this.Name = Name;
this.posX = posX;
this.posY = posY;
this.sizeX = sizeX;
this.sizeY = sizeY;
this.regColor = regColor;
this.highlightColor = highlightColor;
this.buttonColor = regColor;
}
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;
}
}
boolean over()
{
if( overRect(posX, posY, sizeX, sizeY) ) {
over = true;
return true;
}
else {
over = false;
return false;
}
}
void display()
{
stroke(0);
fill(buttonColor);
rect(posX, posY, sizeX, sizeY);
fill(0);
textAlign(CENTER);
text(Name,posX+sizeX/2,posY+sizeY/2+5);
}
}
1