We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everybody,
I am rewriting some code for P3. My aim is to fill up a PImage object with input image file data, selected with the new select-methods (such as https://processing.org/reference/selectInput_.html) and afterwards adjust the surface size depending on this PImages width and height.
This is an exampe code:
PImage input;
void setup() {
size (200, 200);
surface.setResizable(true);
selectInput("Select an image to process", "imageSelected");
surface.setSize(input.width, input.height);
}
void imageSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
input = loadImage (selection.getAbsolutePath());
}
}
It seems the interpreter can not wait until an input-file is picked (No clue about the selection window). It instantly neeeeeeds to run the setSize method - and crash (throws NullPointerException, because "input.width" is not available, because he wants it way to soon)! Is there another solution to my attempt? I think it is a decent desire, to set surface properties as a function of selected input. At least it is what my client desires ;)
Cheers!
Answers
Have you seen/tried this? I haven't but would start there...
https://github.com/processing/processing/wiki/Changes-in-3.0#things-that-may-break-your-2x-sketches
a teaser quote from there:
Yes, I have. That's exactly what I am doing in the code I posted in my question. Not working properly.
Sorry I misread your post. :-S
You could reszie the window when/after an image has been selected.
Maybe anwhile (input==null){selectInput...}
would work. Halting the execution until you get an image, but it halts execution :( .Or even better do call
surface.setSize()
from callbackimageSelected()
if the image is not null.That'd create infinite selectInput() and crash the whole computer! >:)
A better plan: *-:)
Inside the callback:
Thanks a lot for your help! I used the pattern by @benja - it works. Did not try out yours, @GoToLoop. Is it more clean? I mean, I got it "fixed somehow", but deep inside me I wanna be 100% aware about what exactly happens there and why it did "jump" over some code, when I expected it to halt the interpretation, while performing the input selection. Can someone explain? I guess afterwards this topic could be closed. Thank you, guys and girls! Great community, great software! :*
ops sorry
selectInput()
opens a file-chooser dialog, but the dialog is created in a different thread. So you code will not halt. And that's the reason why we use a callback-function ( i.e. imageSelected() ).If you want the code to wait, you would have to handle it yourself. That is what the example of @GoToLoop does.
Hey there,
I just recently found a very simple solution for this in the ASDF Pixelsorting Sketch by Kim Asendorf (https://github.com/kimasendorf/ASDFPixelSort).
He simply did it like this:
Hope it helps, cheers.
@isaakwit This is an old post. I doubt the owner is still around. In any case, currently you can use settings() to load an image and define the size of the sketch there:
Kf
Woah, didn't stumble across that function until now. Thanks a lot!