Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 15-4: Image sequence

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

// Declaring an array of images.
PImage[] images = new PImage[maxImages];

void setup() {
  size(200,200);
  
  // Loading the images into the array
  // Don't forget to put the JPG files in the data folder!
  for (int i = 0; i < images.length; i ++ ) {
    images[i] = loadImage( "animal" + i + ".jpg" ); 
  }
  frameRate(5);
}

void draw() {
  
  background(0);
  image(images[imageIndex],0,0);
  
  // increment image index by one each cycle
  // use modulo " % "to return to 0 once the end of the array is reached
  imageIndex = (imageIndex + 1) % images.length;
}

  • Tea

    Hi, how can you implement the same thing…only difference is no loop? or to show the last image after one sequence?

  • http://www.learningprocessing.com Daniel Shiffman

    Change how you are incrementing imageIndex, i.e. don’t just do it every time through draw(). Build some logic with a conditional statement, etc.

  • Lucy

    I keep getting anull pointer acception in the draw section. I have tried changing the imageIndex to 1 and that allows the program to run through draw once sucecsfully before stopping, and does not load the final image.

  • http://henriquestabile.tumblr.com Henrique Stabile

    This is all really great Daniel, thank you so much!