sorry i can not help because your code can not be tested in my environment
but current version also with random color:
code:
//https://discourse.processing.org/t/random-position-without-overlapping/7904/8
//-1- make a class for a rectangle
//-2- make a arraylist for many rectangles
//-3- create "many" rectangles as array of this class
// using random x y w h
//-4- but after make that random set and before adding that rectangle to the list
// check if it would overlap a existing one ( collision rect rect )
//-5- but if that "while overlap make new set " not finds a good new set
// like when canvas is full...
// check on a time out / break the "while" loop / break the "for many" loop /
//-6- also random color
// array list of class
class MyRect {
int x, y, w, h;
color rstroke, rfill;
MyRect(int x, int y, int w, int h, color rstroke, color rfill) {
this.x=x;
this.y=y;
this.w=w;
this.h=h;
this.rstroke=rstroke;
this.rfill=rfill;
}
void draw() {
push();
fill(rfill);
stroke(rstroke);
rect(x, y, w, h);
pop();
}
}
ArrayList<MyRect> myrects = new ArrayList<MyRect>();
int x, y, w, h;
int many =120; // about 100 OK
boolean avoid_collide = true; //false;
long stime, dtime=1000; // timeout on while
color rf = color(0, 200, 0);
color rs = color(0, 0, 200);
void random_col() {
int R_low = 100, R_high = 255;
int G_low = 100, G_high = 255;
int B_low = 100, B_high = 255;
rf = color(random(R_low, R_high), random(G_low, G_high), random(B_low, B_high));
rs = color(random(R_low, R_high), random(G_low, G_high), random(B_low, B_high));
}
void random_set() {
// random rects
w = int(random(10, 30));
h = int(random(10, 30));
x = int(random(0, width-w));
y = int(random(0, height-h));
random_col();
}
void make_myrects() {
boolean goon=true;
myrects = new ArrayList<MyRect>(); // reset
for (int i=0; i<many; i++) {
stime = millis();
random_set(); // init random rect settings
if ( avoid_collide ) while ( collide(x, y, w, h) && goon ) {
random_set(); // try again until not collide with existing
if ( millis() > stime + dtime ) { // check timeout
println("timeout");
goon = false; // stop while
i = many ; // stop for loop
}
}
if (goon ) {
myrects.add(new MyRect(x, y, w, h, rs, rf)); // ok, make it
println("i "+i+" x "+x+" y "+y+" w "+w+" h "+h);
}
}
}
// RECTANGLE/RECTANGLE // http://www.jeffreythompson.org/collision-detection/rect-rect.php
boolean collide(float r1x, float r1y, float r1w, float r1h) { // new random rect to test before we make it!!
if ( myrects.size() > 0) { // check on all existing rects if that NEW ONE would collide
for ( MyRect onerect : myrects) {
if (r1x + r1w >= onerect.x && // r1 right edge past r2 left
r1x <= onerect.x + onerect.w && // r1 left edge past r2 right
r1y + r1h >= onerect.y && // r1 top edge past r2 bottom
r1y <= onerect.y + onerect.h) // r1 bottom edge past r2 top
return true;
}
}
return false;
}
void show_myrects() {
for ( MyRect onerect : myrects) onerect.draw();
}
void setup() {
size(200, 200);
make_myrects();
println("array size: "+myrects.size()+ " of "+many);
println("use mouseclick or key [r] for regenerate, key [c] toggle collision check");
}
void draw() {
background(200, 200, 0);
show_myrects();
}
void mousePressed() {
println("make_myrects:");
make_myrects();
}
void keyPressed() {
if ( key == 'r' ) {
println("make_myrects:");
make_myrects();
}
if ( key == 'c' ) {
avoid_collide = ! avoid_collide;
println("avoid_collide: "+avoid_collide);
}
}