here is the way of doing it… not so nice…
Can you improve it?
Can you see how this works?
// https://discourse.processing.org/t/how-to-load-images-using-an-arraylist/15263
ArrayList<PImage> img = new ArrayList();
// -------------------------------------------------------------------
// processing core functions
void setup() {
//fullScreen();
size(1500, 900);
// load all
for (int i=0; i<9; i++) {
PImage temp = loadImage("img"+i+".jpg"); // load
temp.resize(290, 0); // resize ("290" = image width, "0" means keep ratio)
img.add ( temp ); // add
}
// shuffle (optional)
img=shuffle( img );
background (0);
}
void draw() {
background (0);
draw_grid(img);
statusBar("Hit Space Bar to shuffle");
}
// -------------------------------------------------------------------
// Inputs
void keyPressed() {
if (key == ' ') {
// shuffle
img=shuffle( img );
}
}
// -------------------------------------------------------------------
// Tools
void statusBar(String text1) {
// status Bar
fill(98); // gray
rect(0, height-23,
width, 24);
fill(255); // white
text(text1,
13, height-6);
}
void draw_grid(ArrayList<PImage> arrL1) {
int x0 = 50, y0 = x0, // distance from border
w = 310, h = w, // width and height of a cell / image
off = 0; // offset between images
int k=0; // counter k as index
// nested for loop makes for columns and rows
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (k>=arrL1.size())
return; // leave here
// we calculate the position on the fly
// we check if mouse is inside the small image
if (mouseX > x0+i*(w+off) &&
mouseX < x0+i*(w+off) + arrL1.get(k).width &&
mouseY> y0+j*(h+off) &&
mouseY< y0+j*(h+off) + arrL1.get(k).height ) {
// BIG image
image(arrL1.get(k),
x0+i*(w+off),
y0+j*(h+off),
400, 400 );
} else {
// normal image
image(arrL1.get(k),
x0+i*(w+off),
y0+j*(h+off));
}
k++;
}//for
}//for
}
ArrayList<PImage> shuffle (ArrayList<PImage> arrL1) {
// shuffle
for (int i=0; i<arrL1.size(); i++) {
int randomPosition = int(random(arrL1.size()));
// swap image at position i with image at position randomPosition
PImage temp = arrL1.get(i);
arrL1.set(i, arrL1.get(randomPosition));
arrL1.set(randomPosition, temp);
}
return arrL1;
}
//