Chrisir
Full Member
Offline
Posts: 207
Germany
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); } }