We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Can anyone help with this please?
In main project file:
Map map;
void setup(){
...
map = new Map();
...
}
In Map's constructor, where (members) pgMap is a PGraphic and imgMap is a PImage, I create a graphic draw some stuff on it, save it out then reload it as an image:
...
//save to file
pgMap.save("map.png");
//re-load as an image
imgMap = loadImage("map.png");
...
}
Another of Map's methods:
void renderWorld() {
image(imgMap, 0, 0);
}
If I call map.renderWorld() from draw() I get a null pointer/"map.png" not found error and sure enough map.png hasn't been created. If I comment out the single line in renderWorld() the map gets created normally and the program functions as expected!
I suspected a path issue but I've tried no path (as above), using a relative path to ./data and using an explicit path to /data but none of those work. A separate image used by the program stored in ./data loads from setup() without any path being specified:
loadImage("compass.jpeg");
I've also tried loading Map's image from setup(), rather than within the constructor like so:
map.imgMap = loadImage("map.png");
but that doesn't work either (also tried specifiying paths at that point).
This is on Linux Mint 32 bit with Processing 3.0.2, any help appreciated!
Answers
Why do you need to save it and reload it? You could use the PGraphics instead.
I've tried this:
Worked fine on Processing 2.2.1 but I get a NullPointerException on Processing 3.0.2.
Thanks for that, maybe it's a bug then - I'll probably get fried for saying it but I'm using OpenJDK, are you? I'm saving the images out because I want to resume the program with a previously created image (like a 'save game' feature).
After a little testing I guess this is some sort of malformed file issue caused by pg.save() which means loadImage() can't load it (according to debugger).
Manually copying the image created by your code then overwriting the original in the OS file manager allows the program to work. Maybe Processing's code that saves out a file when it already exists is different to the code when creating a file from scratch...?
No
In this case, don't you need to load the image only if there is a saved game?
Good, it's not related to that then.
I need to save the image when the program is first run and re-load it on subsequent runs. I could probably work round the issue by saving it out some other way.
Thanks for helping.
Yep, that works under 3.0.2 (must admit I didn't know about PGraphics.get() - very useful), although dataPath() is probably a more logical place for resources :D Thanks again.
Actually get() is from PImage. Class PGraphics
extends
PImage:https://Processing.org/reference/PImage_get_.html
And it's a pity (and a total disconnection of Processing users' needs) that dataPath() is undocumented!
Others similar: dataFile(), sketchPath() and sketchFile().
Tip: We can replace
File file = new File(sketchPath("test.png"));
With:
File file = sketchFile("test.png");
And
img = loadImage("test.png");
withimg = loadImage(file.getPath());
Good points - thank you!