We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I have created a sketch that cycles through images, but when I hit the spacebar, I want it to stop cycling through the images and remain on that particular image until I hit the return key. I don't know how to implement that. Here is my code:
String basedir = "/Users/admin/Desktop/Borderscape_OF/clean";
String fileext = ".jpg";
PImage img;
int i = 0;
String[] filenames;
int imgSize = 182;
java.io.File folder = new java.io.File(dataPath(basedir));
java.io.FilenameFilter extfilter = new java.io.FilenameFilter() {
boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(fileext);
}
};
void setup() {
fullScreen();
frameRate(7);
background(0);
//pull from string
filenames = folder.list(extfilter);
img = loadImage(basedir+"/"+filenames[0]);
surface.setSize(img.width, img.height); // Takes the size of the first image in the folder.
}
void draw() {
keyPressed();
img = loadImage(basedir+"/"+filenames[i]);
float x = random(0, 255);
tint(255, x);
image(img, 0, 0);
i++;
if (i + 1 == filenames.length) {
i = 0;
}
}
void keyPressed() {
if (key == 'x') {
basedir = "/Users/admin/Desktop/Borderscape_OF/glitched";
}
if (key == 's') {
basedir = "/Users/admin/Desktop/Borderscape_OF/clean";
}
}
Answers
use a boolean variable that you define before setup()
name it isCycling or so
now in keyPressed on space
' '
set isCycling = false;on RETURN isCycling = true;
This boils down to
in draw
if(isCycling) is short for if(isCycling==true)
Chrisir ;-)
It works!! Thank you for your quick response :)
;-)
technically, you should never load an image in draw() since it runs 60 times per second, so you're loading the same image again and again which slows your computer down a lot.
better make an array of images and fill it once and for all in
setup()
and just use the array indraw()
. (a bit similar to you array filenames)before setup():
at the end of setup()
in draw()
When I tried implementing those changes, it comes up blank with no error message. Here is my code:
I let it sit and I got this:
An OutOfMemoryError means that your code is either using up too much memory because of a bug (e.g. creating an array that's too large, or unintentionally loading thousands of images), or that your sketch may need more memory to run. If your sketch uses a lot of memory (for instance if it loads a lot of data files) you can increase the memory available to your sketch using the Preferences window.
Ok, increased the memory to 5000 and it works now. Yay! Is there a more efficient way to load a high quantity of images that doesn't take up so much memory as in an array? I have about 200 images.
Sorry, I just see your posts now; well done!
You solved it.
Not sure about memory though. You could resize them to make them smaller but i don’t know if this helps
You could load them In the background or just load chunks of 50 or so during run time (from draw)
Not my expertise
I have read elsewhere in the forum that a good way to do this is to use an ArrayList (https://forum.processing.org/two/discussion/767/dynamic-image-array), but I am not sure how to create an ArrayList of images that calls up images from a folder. I have checked the processing referencing page (https://processing.org/reference/ArrayList.html), though I don't know how to change from calling a single object like a particle to calling images from a folder.
I have changed the following before setup:
and have added the following to setup:
but I don't know how to get it to call the images from the folder.
this is your draw
and this is your setup()
both not tested.
The idea
Now, either you just load 200 images in one go and don't care about memory.
Or you load on the fly while browsing the images.
Then in draw you need a mechanism for that.
That means here are the images and you have a position i in draw (the current image, I will denote with "|" here) :
We always want only 10 images in the list so the current image and 9 images further right.
Now in your approach the user can go only right with |
i
in draw().That means when we are only 4 images away from the end, we should load 10 images more. So that is a new function similar to that in setup below but with using i:
Now, also we need to get rid of images left from the position | i.
That needs to be done (before adding) in a new function in a backward for loop
In theory this could be done in a new thread which runs parallel to the main draw().
But your new setup() :
It's working! Thank you for taking the time to answer my queries.
;-)
Spoke too soon :) It's cycling through the ten images, but where do I put the backwards remove loop?
THE backward for loop could be directly before the for loop to adding images
To check the size() of ArrayList: could be println(...list.size()); in draw()
I did print line and it cycles through ten, but it is still the same 10 images.
I have my set up as follows:
Maybe I haven't declared i correctly? I just have it before setup as
int i;
??
Lines 9 to 16 belong in draw () !!!
The idea is that we load images throughout but not always but only when i + 2 > upper bound
This is a demonstration managing multiple images, version 1.
Kf