PShape[] squares = new PShape[3];
void setup() {
size(300, 300);
// Creating the PShape as a square. The
// numeric arguments are similar to rect().
squares[0] = createShape(RECT, random(200), random(200), random(50+100), random(50+100));
squares[0].setFill(color(random(255), 0, random(255)));
squares[1] = createShape(RECT, random(200), random(200), random(50+100), random(50+100));
squares[1].setFill(color(random(255), 0, random(255)));
squares[2] = createShape(RECT, random(200), random(200), random(50+100), random(50+100));
squares[2].setFill(color(random(255), 0, random(255)));
}
void draw() {
// and a for loop, that takes each square from the list
for ( PShape currentSquare : squares) {
shape(currentSquare);
}
}
Second Example
Of course, you also can make a for loop in setup() to make it even shorter
PShape[] squares = new PShape[3];
void setup() {
size(300, 300);
// we use a for loop to fill the slots of the array
for (int i=0; i<squares.length; i++) {
// Creating the PShape as a square. The numeric arguments are similar to rect().
squares[i]= createShape(RECT, random(200), random(200), random(50+100), random(50+100));
squares[i].setFill(color(random(255), 0, random(255)));
}
}
void draw() {
// and a for loop that takes each square from the list
for (PShape currentSquare : squares) {
shape(currentSquare);
}
}
unfortunately, the nice form of a for-loop as in draw() cannot be used in setup() since the array is empty in setup() and we first have to fill it.