Looking for more resources about ArrayLists

The core idea is that

  • we replace one cell with four cells that fill the space of the old cell
  • the calculation of the pos and size of the four new cells is done in mousePressed() and then just brought into the four new cells into the ArrayList. You had calculations in the class SubGrid . Not needed.
  • Did you notice that you can (like in the image) click in a cell, and then click in a sub-cell and then again in a sub-cell and so on?

Chrisir

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

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

  final float w = width/4; 
  final float h = w;
  final color WHITE = color(255);

  for (float x = 0; x <= width; x+=w) {
    for (float y = 0; y <= height; y+=h) {
      grid.add(new GridCell(x, y, 
        w, h, 
        WHITE));
    }//for
  }//for
}//func

void draw() {
  background (255);

  for (int i = 0; i < grid.size(); i++) { // can use short form of for-loop here
    grid.get(i).display();
  }

  // need a backward for-loop to remove dead ones here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  // can't use short form of for-loop here
}//func 

//---------------------------------------------------------------------------------

void mousePressed() {
  // search clicked cell:
  for (GridCell oldCell : grid) {  // short form of for-loop here
    // check mouse 
    if ( oldCell.onMouse() ) {
      // replace cell with 4 cells 
      grid.add(new GridCell(oldCell.x, oldCell.y, oldCell.w/2, oldCell.h/2, getRandomColor()));              // upper left corner (old pos)
      grid.add(new GridCell(oldCell.x+oldCell.w/2, oldCell.y, oldCell.w/2, oldCell.h/2, getRandomColor()));  // upper right corner 
      grid.add(new GridCell(oldCell.x, oldCell.y+oldCell.w/2, oldCell.w/2, oldCell.h/2, getRandomColor()));  // lower left corner 
      grid.add(new GridCell(oldCell.x+oldCell.w/2, oldCell.y+oldCell.w/2, oldCell.w/2, oldCell.h/2, getRandomColor())); // lower right corner
      // kill old cell
      oldCell.isDead=true;
      // leave here (we found ONE cell, that's enough)
      return;
    }//if
  }//for
}//func 

//---------------------------------------------------------------------------------

color getRandomColor() {
  return
    color(random(255), random(255), random(255));
}//func

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

class GridCell {

  float x, y; // pos
  float w, h; // size 
  boolean isDead = false; 
  color col1     = color(255);

  // constr 
  GridCell(float x_, float y_, 
    float w_, float h_, 
    color col1_) {
    x = x_;
    y = y_;
    w = w_;
    h = h_;
    col1 = col1_;
  }// constr 

  void display() {
    if (isDead)
      return; // leave when it's dead - no displaying 

    stroke(0);
    fill(col1);
    rect (x, y, 
      w, h);
  }//method

  boolean onMouse() { 
    if (isDead)
      return false; // leave with false (mouse is never recognized on a dead cell)

    // this can return true or false: 
    return 
      mouseX>x   &&
      mouseX<x+w &&
      mouseY>y   &&
      mouseY<y+w;
  }//method
  //
}//class 
//
4 Likes