Hey Arie,
I rewrote the program from scratch as that was more efficient. Some remarks:
- I've used a photo class
- still using your assumption that the images are 320 x 240
- the program currently reads jpg and png files from the data directory
- loads all existing images in setup
- checks for new images every draw cycle
- only calls loadPhotos when there are indeed new images
- only loads the new images
- only moves photos that haven't reached their destination
- see the code introduction for some more features
Hope this helps.
Amnon
Code:
//***************************************************************//
// Dynamic Image Slideshow
// by amnonP5 <http://amnonp5.wordpress.com>
//
// space = Add slide
// z = Turn ON / OFF the autoplay function
// x = Delete last slide
// c = Clear all slides
//
// Comment IN / OUT: print added image message to console
// Comment IN / OUT: auto-reset slideshow at last slide
//
//***************************************************************//
import processing.opengl.*;
ArrayList photos = new ArrayList();
String[] loadFilenames;
PImage[] loadPhotos;
int counter, fileCount;
boolean autoPlay = false; // by default autoplay is OFF
void setup() {
size(1280,720,OPENGL);
// size(screen.width,screen.height,OPENGL); // full screen mode
smooth();
loadPhotos = new PImage[10000]; // maximum of 10.000 images
loadFilenames();
loadPhotos(); // all existing images are loaded in setup
fileCount = loadFilenames.length;
}
void draw() {
background(51);
checkNew(); // checks for new images in the data directory every draw cycle
if(autoPlay) {autoPlay();}
for (int i = 0; i < photos.size(); i++) {
Photo s = (Photo) photos.get(i);
s.display();
s.move();
}
// if (photos.size() == fileCount) {photos.clear();} // reset slideshow when it reaches the last slide
}
void loadFilenames() {
java.io.File folder = new java.io.File(dataPath("")); // reads files from data folder
java.io.FilenameFilter imgFilter = new java.io.FilenameFilter() {boolean accept(File dir, String name) {return name.toLowerCase().endsWith(".jpg") || name.toLowerCase().endsWith(".png");} };
loadFilenames = folder.list(imgFilter);
}
void loadPhotos() {
for (int i = fileCount; i < loadFilenames.length; i++) { // only load new images
loadPhotos[i] = loadImage(loadFilenames[i]);
// println("Photo added: " + loadFilenames[i]); // print added image message to console
}
}
void checkNew() {
loadFilenames();
if (loadFilenames.length > fileCount) {loadPhotos(); fileCount = loadFilenames.length;} // only call loadPhotos if there are new images
}
void autoPlay() {
counter++;
if (counter >= 60 && photos.size() < loadFilenames.length) {photos.add(new Photo(random(80,width-80),random(60,height-60),random(-30,30))); counter=0;}
}
void keyPressed() {
if (key == 'z') {if(autoPlay) {autoPlay = false;} else {autoPlay = true;}}
if (key == ' ') {if (photos.size() < loadFilenames.length) {photos.add(new Photo(random(80,width-80),random(60,height-60),random(-30,30)));}}
if (key == 'x') {if (photos.size() > 0) {photos.remove(photos.size()-1);}}
if (key == 'c') {photos.clear();}
}
class Photo {
PImage photo;
float targetXpos;
float targetYpos;
float rotation;
float xpos;
float ypos;
float easing;
Photo(float targetXposTemp, float targetYposTemp, float rotationTemp) {
photo = loadPhotos[photos.size()];
xpos = width/2;
ypos = -130;
targetXpos = targetXposTemp;
targetYpos = targetYposTemp;
rotation = rotationTemp;
easing = 0.1;
}
void display() {
rectMode(CENTER);
imageMode(CENTER);
noStroke();
pushMatrix();
translate(width/2,height/2);
rotate(radians(rotation));
translate(-width/2,-height/2);
translate(xpos,ypos);
fill(0,0,0,50);
rect(4,4,330,250);
fill(255,255,255);
rect(0,0,330,250);
image(photo,0,0,320,240);
popMatrix();
}
void move() {
if (dist(xpos,ypos,targetXpos,targetYpos) > 1) { // only move if the destination has not been reached
xpos = (1-easing) * xpos + (easing) * targetXpos;
ypos = (1-easing) * ypos + (easing) * targetYpos;
}
}
}