Looking for more resources about ArrayLists

This is where I’ve arrived. MousePressed is set to add subGrid cells with each click.

However this is currently not displaying properly. And I can’t figure out why.
In this case, I am aiming to have no extra space between the subGrid cells.

The color is not really important, only being used as an indicator of subCell positions.

My primary question at this point is why the subCells are not always abutting each other? Some are and others are not…
:thinking:

ArrayList <GridCell> grid;
ArrayList <SubGrid> sub;
float w;
float y = 0;

void setup() {
  size (800, 400);
  noLoop();
  background (255);

  grid = new ArrayList <GridCell>();
  sub = new ArrayList <SubGrid>();

  w = width/8; 
  float h = height;

  for (float x = 0; x <= width; x+=w) {
    grid.add(new GridCell(x, y, w, h));
  }
}
void draw() {
  
  float h = width/8;

  for (int i = 0; i < grid.size(); i++) {
    grid.get(i).display();
  }  

  for (int j = 0; j < sub.size(); j++) {
    sub.get(j).display();
  }
}
void mousePressed() {

  sub = new ArrayList <SubGrid>();
  
  float w = width/8;
  float h = w;

  for (int i = 0; i < grid.size(); i++) {
    sub.add(new SubGrid(grid.get(i).x, y, w, h));
  }
  redraw();
  y = y+100;
}

MAIN GRID CLASS //////////////////////////////////

class GridCell{
  float x, y;
  float w, h;
  
  GridCell(float x_, float y_, float w_, float h_){
    x = x_;
    y = y_;
    w = w_;
    h = h_;
  }
  
  void display(){
    stroke(0);
    noFill();
    rect (x, y, w, h);
  }

SUBGRID CLASS //////////////////////////////

class SubGrid {
  float x, y;
  float w, h;

  SubGrid(float x_, float y_, float w_, float h_) {
    x = x_;
    y = y_;
    w = w_;
    h = h_;
  }

  void display() {
    stroke(0);
    fill(color(random(255), random(255), random(255)), 50);
    rect (x, y, w/2, h/2);
    rect (x, y/2, w/2, h/2);
    rect (x/2, y, w/2, h/2);
    rect (x/2, y/2, w/2, h/2);
  }
}
1 Like