image slideshow by amnon owed help
in
Programming Questions
•
2 years ago
Hi there, i found this fantastic code by amnon while searching for a slideshow.
This code is great and works as excpetdec i just have two little problems i cant figure out.
First is that the code doesnt work in the latest processing versios. but works fine when using older ones.
i guess it misses some java libraries that has to be loaded. but how do i know which one?
oh and the second is the way it works.it loads all images in one folder and place them on the screen. this is pretty cool, but lets say i have only 4 images. it stops but what i would love to do is, if the last image is placed it should begin again without removing the other images, so its getting more and more. this has two benefits. first it looks good when you only have a limited number of images and if you have a little more, it starts again frmo the beginning and you get to see the other images as well and you can actually run the code forever which would be cool instead of just showing every picture only once. maybe it can also be random... can any body give me some hints. that would be great.
here is the code.
This code is great and works as excpetdec i just have two little problems i cant figure out.
First is that the code doesnt work in the latest processing versios. but works fine when using older ones.
i guess it misses some java libraries that has to be loaded. but how do i know which one?
oh and the second is the way it works.it loads all images in one folder and place them on the screen. this is pretty cool, but lets say i have only 4 images. it stops but what i would love to do is, if the last image is placed it should begin again without removing the other images, so its getting more and more. this has two benefits. first it looks good when you only have a limited number of images and if you have a little more, it starts again frmo the beginning and you get to see the other images as well and you can actually run the code forever which would be cool instead of just showing every picture only once. maybe it can also be random... can any body give me some hints. that would be great.
here is the 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 = true; // 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,640,480);
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;
}
}
}
1