new version of same Sketch
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?
// New version
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);
// display all cells
for (GridCell currentCell : grid) { // can use short form of for-loop here
currentCell.display();
}//for
// backward for-loop to remove dead ones here !!
// can't use short form of for-loop here
// remove dead ones (you need to have a conventional for-loop here, backwards)
for (int i=grid.size()-1; i>=0; i--) {
if (grid.get(i).isDead)
grid.remove(i);
}//for
}//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(oldCell.getUpperLeftCorner()); // upper left corner (old pos)
grid.add(oldCell.getUpperRightCorner()); // upper right corner
grid.add(oldCell.getLowerLeftCorner()); // lower left corner
grid.add(oldCell.getLowerRightCorner()); // 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
//--- make child cells (all with w/2)
GridCell getUpperLeftCorner() {
// upper left corner (old pos)
return new GridCell(x, y,
w/2, h/2, getRandomColor());
} //method
GridCell getUpperRightCorner() {
// upper right corner (x changed)
return new GridCell(x+w/2, y,
w/2, h/2, getRandomColor());
} //method
GridCell getLowerLeftCorner() {
// lower left corner (y changed)
return new GridCell(x, y+w/2,
w/2, h/2, getRandomColor());
} //method
GridCell getLowerRightCorner() {
// lower right corner (x and y changed)
return new GridCell(x+w/2, y+w/2,
w/2, h/2, getRandomColor());
} //method
//
// ---
//
}//class
//