Random Image Processor, Crash Solve

edited February 2016 in Questions about Code

Hey, I'm trying to make a generative art project using the mouse click to display 2/3 image on the screen, selecting randomly from an array to generate new compositions ect

the base code i've been working from is here but is not in working condition. https://forum.processing.org/two/discussion/7960/#Comment_29719

I've fiddled with it to create a code that opens the sketch but crashes on the first click.

int maxImages = 5; // Total # of images
int imageIndex = 0; // Initial image to be displayed is the first

// Declaring an array of images.
PImage[] a = new PImage[maxImages]; 
PImage[] b = new PImage[maxImages]; 
PImage[] c = new PImage[maxImages]; 
PImage[] d = new PImage[maxImages]; 
PImage[] e = new PImage[maxImages]; 

void setup() {
  size(1920,1080);

  // Loading the images into the array
  // Don't forget to put the JPG files in the data folder!
  for (int i = 0; i < a.length; i ++ ) {
    a[i] = loadImage( "0.jpg" ); 
  }
  for (int i = 0; i < b.length; i ++ ) {
    b[i] = loadImage( "1.jpg"); 
  }
  for (int i = 0; i < c.length; i ++ ) {
    c[i] = loadImage( "2.jpg" ); 
  }
  for (int i = 0; i < d.length; i ++ ) {
    d[i] = loadImage( "3.jpg" ); 
  }
  for (int i = 0; i < e.length; i ++ ) {
    e[i] = loadImage( "4.jpg" ); 
  }
}

void draw() {
  // Displaying images
  image(a[imageIndex],0,0); 
  image(b[imageIndex],0,height/3*2);
  image(c[imageIndex],0,height/3*3);
imageIndex += 1;
//imageIndex = constrain (imageIndex, 0,0);
imageIndex = constrain (imageIndex, 0, height/3*2);
imageIndex = constrain (imageIndex, 0, height/3*3);
}


void mouseReleased() {
  // new images picked randomly when released
  imageIndex = int(random(a.length));
  imageIndex = int(random(b.length));
  imageIndex = int(random(c.length));
}

it calls "nullvoid" on " image(a[imageIndex],0,0); "

thanks in advance

,,

Tagged:

Answers

  • ****** i'm now receiving an "arrayIndexOutOfboundsExpection: 5"

  • So, first question: How many images are you trying to use? Five? Twenty five?

  • lack of understanding arrays?

    you are loading 5 times the same image 0.jpg into array a and so on....

  • imageIndex = constrain (imageIndex, 0, height/33); ??????

    you mean

    imageIndex = constrain (imageIndex, 0, maxImages );

    and why this:

    imageIndex += 1; ???

  • Please format the code. Highlight it, press Ctrl o

  • edited February 2016

    @TfGuy44 i want to be able to have a large amount of images, maybe 25 or 30.

  • edited February 2016

    Alright. Next question: Let's say you have 25 images. What sort of a data structure would you put them in?

    BONUS: This question is multiple choice!

    A) Five arrays with five images each.

    B) 25 differently named individual PImage variables.

    C) One array of 25 PImages.

    D) A Binary Tree, of course! Trees are awesome!

  • @tfguy44

    I would assume c) one array of 25 PImages for random to work, but i've never heard of a Binary Tree in Processing!

  • edited February 2016

    answer c) is correct!

    So rewrite your code so that you have only one array with different images

    you can retrieve the name as str(i)+".jpg"

    that is the idea of an array. It's a list (like a shopping list) with multiple lines.

    at each line stands a different (!) image

Sign In or Register to comment.