Arranging an array of objects in a grid

here :

Button(float tempx, float tempy,

float tempw, float temph, 

color tempCol, 

float tempSz) {

we use

  • tempW and temph (w and h) in the class only in click();
  • sz only in display().

You might fix that.


This part is superfluent I guess:

    //checking for edges

    if (x+sz>=width)  

      x=width-sz-1;  

    if (y+sz>=height)

      y=height-sz-1;

Color :

You can make it more colorful using: color(random(255), random(255), random(255))

  myButtons [k] = new Button (
    x + i * (w + off), y + i2 * (h + off), 
    w, h, 
    color(random(255), random(255), random(255)), 
    random(50, 110)); // size

Click

Your mouse click doesn’t work anymore

    if (mouseX > x && 
      mouseX < x + w && 
      mouseY > y && 
      mouseY < y + h)

because it assumes that you don’t use rectMode(CENTER); !!!

This should work with rectMode(CENTER) :

  void click() {
    if (mouseX > x - w/2 && 
      mouseX < x + w/2 && 
      mouseY > y -h/2 && 
      mouseY < y + h/2) 
      on = !on; // toggle
  }
2 Likes