@hacketet. It would be nice if you could post a runnable example. In the meantime and for whatever it’s worth, the following example may do something similar to what you are trying to achieve.
/*
Creates a grid of push buttons from a generic Button class array.
Syntax: btn[id] = new Button( x, y, w, h, "title", bkgrndColor, txtColor);
ID is taken from position in array.
*/
final int _btnGridX = 40;
final int _btnGridY = 60;
final int _btnW = 120;
final int _btnH = 60;
color BLUE = color(64,124,188);
color LTGRAY = color(185,180,180);
color YELLOW = color(245,250,13);
color RED = color(255,0,0);
color BLACK = color(0,0,0);
color WHITE = color(255,255,255);
Button[] btn;
class Button {
float x, y, w, h;
String title;
color bkgrnd;
color txtColor;
// Constructor
Button(int xpos, int ypos, float wt, float ht, String titleStr, color background, color foreground) {
x = xpos;
y = ypos;
w = wt;
h = ht;
title = titleStr;
bkgrnd = background;
txtColor = foreground;
}
void display(){
fill(bkgrnd); // background color
noStroke();
rect(x, y, w, h, 15); // rounded rect
fill(txtColor); // text color
textSize(42);
textAlign(CENTER, CENTER);
text(title, x, y, w, h);
}
void press(int id){
println("btn id = ", id);
}
}
void btnGrid() {
int left = 0;
int top = 0;
int vg = 30; // Space between cols (vert.gutter)
int hg = 30; // Space between rows (horz.gutter)
int rows = 5;
int cols = 5;
int id = 0;
btn = new Button[rows*cols]; // creates button array
for (int k = 0; k < cols; k++) {
for (int j = 0; j < rows; j++) {
left = _btnGridX + j * (_btnW + vg);
top = _btnGridY + k * (_btnH + hg);
btn[id] = new Button(left, top, _btnW, _btnH, str(id), LTGRAY, BLACK);
id++;
}
}
}
void setup() {
size(800,600);
background(BLUE);
btnGrid();
}
void draw() {
for (int i = 0; i < btn.length; i++) {
btn[i].display();
}
}
void mousePressed() {
for (int i = 0; i < btn.length; i++) {
if((mouseX >= btn[i].x) && (mouseX <= btn[i].x + _btnW) && (mouseY >= btn[i].y) && (mouseY <= btn[i].y + _btnH)) {
btn[i].press(i);
}
}
}