I’m trying to further my understanding of how to implement an object array with this simple sketch.
- I created a while loop within a while loop to create the grid.
- I added the object array
- But the screen appears empty without an error message when i run the sketch.
It’s probably a very simple oversight, but I can’t figure out what I’ve done wrong.
Code below /////////////////////////////////////////////
Button [] myButtons = new Button [25];
void setup() {
size (600, 600);
for (int i = 0; i < myButtons.length; i++) {
myButtons [i] = new Button (int(random(255)), random(25, 50));
}
}
void draw() {
background (255);
float x = 0;
float y = 0;
for (int i = 0; i < myButtons.length; i++) {
//float x = 0;
while (x < width) {
//float y = 0;
while (y < height) {
myButtons[i].display();
}
}
y = y + 25;
}
x = x + 25;
}
void mousePressed() {
for (int i = 0; i < myButtons.length; i++) {
myButtons[i].click();
}
}
CLASS ///////////////////////////////////////////////////////////
class Button {
float x;
float y;
color col;
float sz;
boolean on;
Button(color tempCol, float tempSz) {
col = tempCol;
sz = tempSz;
//checking for edges
if (x+sz>=width)
x=width-sz-1;
if (y+sz>=height)
y=height-sz-1;
// button starts in the off position
on = false;
}
void display() {
// fill + stroke appearance when button is on / off
if (on) {
fill (255);
stroke(0);
} else {
fill (0);
stroke (255);
}
rect (x, y, sz, sz);
}
void click() {
if (mouseX > x && mouseX < x + sz && mouseY > y && mouseY < y + sz) {
on = !on;
}
}
}
Any guidance most welcome and appreciated!!