I have worked for a while to make a paint-like program. The area that i use to draw is a Canvas. Now, i want to save whatever i draw on it ...to a file on my harddisk. I have tried like this:
public class SaveJPEG extends Thread { SuprafataDesenare plansa; // this is the class which extends Canvas String url; // this is a string which is the path to the file FereastraPrincipala fp; // this is the main Frame of my application public SaveJPEG(SuprafataDesenare plansa, String url,FereastraPrincipala fp) { this.plansa = plansa; this.url = url; this.fp = fp; } public void run() { try { BufferedImage image = new BufferedImage(plansa.getWidth(), plansa.getHeight(), BufferedImage.TYPE_INT_RGB); BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(url)) ; Graphics2D graphics = image.createGraphics(); plansa.paintAll(graphics); ImageIO.write(image, "jpg", fos); fos.close(); } catch(Exception e) { e.printStackTrace(); } } }
But it looks like it does not work . I have seen a code like this on a site, and there was said that it should save the content of my canvas to a .jpg file. Sadly, it creates a blank .jpg file. Is there something that i have to change at my code to make it work? Is there any other way that i can save the content of my paint ?