Loading...
Logo
Processing Forum
Hi,
I would like some tips on how to view images in a grid format in Processing? Would I use an ArrayList and store all the images in there or would I just use a for Loop. Please can someone advise me on the best way to approach this solution,

Thank you.


Replies(5)

Have you tried anything yet? there are a variety of ways of handling this. You just really need a kind of collection (array, arraylist, etc) and a smartly written for loop or nested for loop that keeps track of the dimensions of the images.
  for (int i=0; i<images.length;i++){
    images[i] = loadImage(""+(i+1)+".jpg");
    tiles[i] = new Tile(x, y, gridSpacing, images[i], i);
    x += gridSpacing;
    tiles[i+images.length] = new Tile(x, y, gridSpacing, images[i], i);
    x += gridSpacing;
    if (x > gridWidth){
      y = y + gridSpacing;
      x = 0;
    }
This is a for loop but do you think there is a way where you can create a class grid...where you call the grid cell and input an image. 
I have included a project i made a while ago that creates a grid of images from a video feed. Theres a class called positions thats just and x and a y and it gets populated from a for loop that calculates the points in the grid then the images are placed based on an array of positions.

sorry for not parsing and pulling what you need out of it i'm in a bit of a hurry right now haha but i hope this helps

K.


Copy code
  1. import processing.video.*;

  2. positions[] pos = new positions[100];
  3. int randomInd;
  4. int imgSpotX; 
  5. int imgSpotY;

  6. Capture cam;
  7. int posCount = 0;


  8. void setup() {
  9.   size(screen.width, screen.height);
  10.   frameRate(25);
  11.   cam = new Capture(this, screen.width/10, screen.height/10, 30);

  12. //////for loop to populate the the position classes

  13.   for (int i = 0; i<width; i+=(width/10)) {
  14.     for (int j = 0; j < height; j+=(height/10)) {
  15.       pos[posCount] = new positions(i, j);
  16.       posCount++;
  17.     }
  18.   }
  19. }

  20. ///////////////////

  21. void draw() {
  22.   randomInd = floor(random(pos.length));
  23.   imgSpotX = pos[randomInd].x;// place image according to positions array
  24.   imgSpotY = pos[randomInd].y;//place image according to positions array
  25.   image(cam, imgSpotX, imgSpotY);
  26. }

  27. void captureEvent(Capture myCap) {
  28.   myCap.read();
  29. }

  30. void keyPressed() {
  31.   saveFrame("shot-####");
  32. }


  33. class positions{
  34. public int x;
  35. public int y;

  36. public positions(int inX, int inY){
  37.   x = inX;
  38.   y = inY;
  39. }
  40. }//end of class


" Would I use an ArrayList and store all the images in there or would I just use a for Loop."
If you use an ArrayList, you still need a for loop to display the images!
An array list isn't necessary if you know in advance the number of images and don't plan to change it.

" create a class grid...where you call the grid cell and input an image."
Why not, it can make the code tidier. But the loop you shown will still be in the grid class. Perhaps it can delegate the loading to the grid cell class, either way is fine.

About your code: why do you create two Tile objects per image?
@PhiLho The reason it creates two tile objects is because I am trying to create a matching cards game, so basically it will display two random pairs and the player has to guess where the matching pairs are.

@Kobby Thank you so much for sharing that I shall have a look at it and see what I can do.

I think I'd prefer creating a class grid