We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Array for RectButtons
Page Index Toggle Pages: 1
Array for RectButtons? (Read 595 times)
Array for RectButtons?
Oct 5th, 2009, 10:26am
 
Hello,
I have a simple question on RectButtons.
For several reasons, I'd like to have one
Array for RectButtons and another one with two dimensions for RectButtons.

Is that Possible?

At the moment I have
   ...
   RectButton[] rectBt = new RectButton[7];
   ...
   rectBt[i] = new RectButton(150, 20, 100, buttoncolor, highlight);
and receive an error...

Thanks -

Greetings,
Chris

****


// ==========================================================

class Button
{
 int x, y;
 int size;
 color basecolor, highlightcolor;
 color currentcolor;
 boolean over = false;
 boolean pressed = false;  

 void update()
 {
   if(over()) {
     currentcolor = highlightcolor;
   }
   else {
     currentcolor = basecolor;
   }
 }

 boolean pressed()
 {
   if(over) {
     locked = true;
     return true;
   }
   else {
     locked = false;
     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(int ix, int iy, int isize, color icolor, color ihighlight)
 {
   x = ix;
   y = iy;
   size = isize;
   basecolor = icolor;
   highlightcolor = ihighlight;
   currentcolor = basecolor;
 }

 boolean over()
 {
   if( overRect(x, y, size, size) ) {
     over = true;
     return true;
   }
   else {
     over = false;
     return false;
   }
 }

 void display()
 {
   stroke(255);
   fill(currentcolor);
   rect(x, y, size, size);
 }
}

Re: Array for RectButtons?
Reply #1 - Oct 5th, 2009, 10:56am
 
It would be much easier to help if you included all the code - particularly where you instantiate your RectButton objects, and the exact nature of the error message you receive.  Not sure I follow the question about a two dimensional array of RectButtons.  Is this what you want to do:

Code:
int btnLength = 5;
// This needs to be declared outside of setup to ensure it has global scope.
RectButton[] rectBt = new RectButton[btnLength];
boolean locked;

void setup() {
 size(200,200);
 color col1 = #ff0000;
 color col2 = #ffff00;
 for(int i=0; i<btnLength; i++) {
     // Using a multiple of i means the buttons aren't all on top of each other
     rectBt[i] = new RectButton(40 * i, 20, 30, col1, col2);  
 }  
}

void draw() {
   for(int i=0; i<btnLength; i++) {
       rectBt[i].display();
       rectBt[i].update();
   }
}



class Button {
int x, y;
int size;
color basecolor, highlightcolor;
color currentcolor;
boolean over = false;
boolean pressed = false;  

void update()  {
  if(over()) {
    currentcolor = highlightcolor;
  }
  else {
    currentcolor = basecolor;
  }
}

boolean pressed()  {
  if(over) {
    locked = true;
    return true;
  }
  else {
    locked = false;
    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(int ix, int iy, int isize, color icolor, color ihighlight)  {
  x = ix;
  y = iy;
  size = isize;
  basecolor = icolor;
  highlightcolor = ihighlight;
  currentcolor = basecolor;
}

boolean over()  {
  if( overRect(x, y, size, size) ) {
    over = true;
    return true;
  }
  else {
    over = false;
    return false;
  }
}

void display()  {
  stroke(255);
  fill(currentcolor);
  rect(x, y, size, size);
}
}
Re: Array for RectButtons?
Reply #2 - Oct 5th, 2009, 10:59am
 
The code you pasted works fine.  Need to know the exact error you're getting.  Offhand I'd say: are buttonColor and buttonHilight valid color variables?  Is your rectBt array declared globally?  Is your array being filled properly (not exceeding bounds)?
--Ben
Re: Array for RectButtons?
Reply #3 - Oct 10th, 2009, 2:37pm
 
Hello !

Thank you both for your reply!
You helped me a lot.

I post two complete codes based on yours.
Both have Mouse-Over.
The first code with an one-dimensional array. The buttons are now
clickable, Button-Number is printed in the Output-Window when clicked.

The second code with an two-dimensional array. The buttons are
clickable as well, Button-Number (x,y) is printed in the Output-Window when clicked.

A few Buttons are offline, meaning they don't exist. In fact, they exist, but are invisible and without function.

Thanks again!

Greetings, Chrisir

First-----------------------------------------------------------

int btnLength = 5;
// This needs to be declared outside of setup to ensure it has global scope.
RectButton[] rectBt = new RectButton[btnLength];
boolean locked;

void setup() {
 size(400,200);
color col1 = #ff0000;
color col2 = #ffff00;
 for(int i=0; i<btnLength; i++) {
   // Using a multiple of i means the buttons aren't all on top of each other
   rectBt[i] = new RectButton(40 * i + 11, 20, 30, col1, col2);  
 }  
}

void draw() {
 for(int i=0; i<btnLength; i++) {
   rectBt[i].display();
   rectBt[i].update();
 }
}

void mouseReleased()
{
 if  (mouseButton == LEFT) {
   for(int i=0; i<btnLength; i++) {
     if (rectBt[i].pressed()) {  
       println(i);  
     };

   }
 }
}

class Button {
 int x, y;
 int size;
 color basecolor, highlightcolor;
 color currentcolor;
 boolean over = false;
 boolean pressed = false;  

 void update()  {
   if(over()) {
     currentcolor = highlightcolor;
   }
   else {
     currentcolor = basecolor;
   }
 }

 boolean pressed()  {
   if(over) {
     locked = true;
     return true;
   }
   else {
     locked = false;
     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(int ix, int iy, int isize, color icolor, color ihighlight)  {
   x = ix;
   y = iy;
   size = isize;
   basecolor = icolor;
   highlightcolor = ihighlight;
   currentcolor = basecolor;
 }

 boolean over()  {
   if( overRect(x, y, size, size) ) {
     over = true;
     return true;
   }
   else {
     over = false;
     return false;
   }
 }

 void display()  {
   stroke(255);
   fill(currentcolor);
   rect(x, y, size, size);
 }
}



Second -------------------------------------------------------

int btnLength = 5;
int btnLength2 = 15;
// This needs to be declared outside of setup to ensure it has global scope.
RectButton[][] rectBt = new RectButton[btnLength][btnLength2];
boolean locked;

void setup() {
 size(800,800);
color col1 = #ff0000;
color col2 = #ffff00;
 for(int i=0; i<btnLength; i++) {
   for(int j=0; j<btnLength2; j++) {
     // Using a multiple of i and j means the buttons aren't all on top of each other
     if (i != j) {
       rectBt[i][j] = new RectButton(40 * i + 19, 25 * j + 19, 20, col1, col2, true);  
     }  
     else {        
       rectBt[i][j] = new RectButton(40 * i + 19, 25 * j + 19, 20, col1, col2, false);  
     }
   }
 }
 rectBt[2][11].Exists = false;
}

void draw() {
 for(int i=0; i<btnLength; i++) {
   for(int j=0; j<btnLength2; j++) {
     if (rectBt[i][j].Exists) {
       rectBt[i][j].display();
       rectBt[i][j].update();
     }
   }
 }
}

void mouseReleased()
{
 if  (mouseButton == LEFT) {
   for(int i=0; i<btnLength; i++) {
     for(int j=0; j<btnLength2; j++) {
       if (rectBt[i][j].pressed()) {  
         print(i  );          
         print(' ');          
         println(j );  
       }
     }
   }
 }
}

class Button {
 int x, y;
 int size;
 color basecolor, highlightcolor;
 color currentcolor;
 boolean over = false;
 boolean pressed = false;  
 boolean Exists = false;  

 void update()  {
   if(over()) {
     currentcolor = highlightcolor;
   }
   else {
     currentcolor = basecolor;
   }
 }

 boolean pressed()  {
   if(over) {
     locked = true;
     return true;
   }
   else {
     locked = false;
     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(int ix, int iy, int isize, color icolor, color ihighlight, boolean iExist)  {
   x = ix;
   y = iy;
   size = isize;
   basecolor = icolor;
   highlightcolor = ihighlight;
   currentcolor = basecolor;
   Exists = iExist;
 }

 boolean over()  {
   if( overRect(x, y, size, size) ) {
     over = true;
     return true;
   }
   else {
     over = false;
     return false;
   }
 }

 void display()  {
   stroke(255);
   fill(currentcolor);
   rect(x, y, size, size);
 }
}

Page Index Toggle Pages: 1