2D grid array question

This

int r = int(random(255));
int g = int(random(255));
int b = int(random(255));


color background = color (r, g, b);

shorter

color background = color (random(255), random(255), random(255));

grid position

You don’t shift the grid as a whole but each cell.

You didn’t use x,y in setup()

int x = 58, y = x;

Now I have

 // Initialize each object
      myButtons[i][j] = new Button (x+ i*(width/cols), y+ j*(height/rows), // USE x and y here

Then get rid of translate, pushMatrix and popMatrix


void draw() {
  background(background);

  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      myButtons[i][j].mouseOver();
      myButtons[i][j].display();
    }
  }
}

and adjust mouseOver()


  if (mouseX > x - sz/2 && 
      mouseX < x + sz/2  &&
      mouseY > y - sz/2  &&
      mouseY < y + sz/2  ) {

Chrisir

1 Like