I have been struggling with an issue involving a Processing sketch which I want to use to create an image file (to a server-side directory). In the Java, I use a PImage of the canvas, converted to a BufferedImage, which is then created as a file via ImageIO.write.
It creates the file successfully when run as a sketch on my system, file.png appears in the same folder as the program. The problem is that when the program is exported as an applet, it no longer creates the file. It would be huge if anyone had any input as to why the sketch of the program can create the image but the applet cannot.
Here's a couple snippets of what I'm using if it's of any help. Thanks all.
PImage pimg = get();
BufferedImage canvas = convertToBufferedImage(pimg);
File file = new File("./sketch/file.png");
try
{
ImageIO.write(canvas, "PNG", file);
}
catch (Exception e) { println(e); }
And here's the method that's converting the PImage to a BufferedImage.
BufferedImage convertToBufferedImage(PImage pimg){
PGraphics pg = createGraphics(width, height, JAVA2D);
pg.image(pimg, 0, 0);
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D g2d = img.createGraphics();
g2d.drawImage((java.awt.Image)pg.image, 0, 0, width, height, this);
g2d.finalize();
g2d.dispose();
return img;
}