Grid of Images PImage error

Hello, I am kind of new to processing and I am building an image grid using 2d array, but I don’t know why I keep getting this error message :"Types mismatch: cannot convert from PImage to Pimage []. I made my research and I couldn’t find a better way to declare the array, can anyone gime a light?
Here’s the code:

PImage[][] images = new PImage[5][5];

void setup() {
  size(1000, 1000);   
  
  frameRate(30);
}

void draw() {

int column = 5;
int rows = 5;
 

  for (int i = 0; i <5; i++){
  PImage img = loadImage(""+i+".png");
  images[i] = img;
}

for(int j = 0; j < 5; j++){
  PImage img1 =  loadImage(""+j+".png");
  images[4+j] = img1;
}
for (int i = 0; i < column; i++) {
  for (int j = 0; j < rows; j++) {
   images[i][j] = loadImage(""+i+".png");
  image(img, 0, 0, rows, column);
  image(img1, rows, column, rows, column);
}
}

}

Function loadImage() returns a PImage object.

However, at the statement images[i] = img;, the images[i] represents an array of PImage objects; b/c variable images is a 2D-array.

images[4+j] = img1; is also wrong! Take a look at the concept of 2D-arrays:

The function setup() gets called one time, but draw() gets called repeatedly. You presumably only want to load your images once, so move all your code that loads the images into setup(), leaving the image() calls in draw(). If, however, you only plan to display the images once and not move or interact with them, you could move the drawing code into setup() as well or add noLoop() to setup() which will cause draw() to be called only once.

But first address what @GoToLoop mentioned. image[][] is an array of arrays. Before you can fill it with images, you have to create each of its 1-D row arrays.

OR

You could replace the 2-D array with a simpler 1-D array of 25 images and do the little bit of math needed to index them into a square yourself. For instance, the image at i,j would be images[i+j*5].

1 Like

This tutorial also discusses this method in the Pixels, pixels, and more pixels section:

:)