| versions | 0095+ |
|---|---|
| contributors | johng |
| started on | 2006-03-09 11:56 |
This is a simple chunk of code to let you have a slideshow without having to use a large number of PImages, which can cause memory problems.
Assuming you have a large number of images you wish to display in a slideshow, the inital reaction is to load all of the images inside setup() and then just loop through the images inside of draw(). However when you have a large number of images, this means a large ammount of memory being used, as each image has to be uncompressed to be able to be drawn.
A way to avoid this is to only load the image you want to draw next. We can do this because if we do not use background() the previous image is already displayed on screen, so we no longer need to have it loaded into a PImage. This way we use a minimal ammount of memory.
There will be some delay in the applet whilst the image is loaded, however if we load it as soon as the previous image has been displayed, there is no problem, as you will want to have that image displayed for longer than the time it takes to load the next (presumably).
/** slideshow taken from http://processinghacks.com/hacks:slideshow @author John Gilbertson Correction: for Processing 114 Beta. by ramin */ PImage img; String[] files; int timer; int nextimg; void setup() { size(400,400); files=new String[3]; //some example images. files[0]="medium.jpg"; files[1]="small.jpg"; files[2]="big.jpg"; nextimg=(nextimg+1)%files.length; img=loadImage(files[nextimg]); timer=0; } int nextimg=0; String[] files; PImage img; int timer; void draw() { if(timer==0) { background(0); image(img,0,0,width,height); } else if(timer==1) { img=loadImage(files[nextimg]); nextimg=(nextimg+1)%files.length; delay(1000); //this decides how long an image should be displayed } timer=(timer+1)%2; }