How to load/save an Image from a JPanel
image, java, javax.imageio, jpanel, swing
Solution
You're question is missing some important information: how are you drawing things on the JPanel? In my opinion, the smart way would be to draw into a BufferedImage, and then simply save the BufferedImage to file using `ImageIO.write(...)`
Edit You state:
I'm just drawing polygons onto a class that extends JPanel. I create them via the paintComponent method.
Again, I recommend that you draw them to a BufferedImage, by getting its Graphics context, drawing to the image using this Graphics object, and then disposing the Graphics object. You would display the BufferedImage in your JPanel's `paintComponent(...)` method by calling `g.drawImage(...)`. Then if you want to save the drawing, again, simply save the BufferedImage.
Problem
I'm missing the export file code, but I have no clue as to what I need to add. I have a drawn image in a JPanel -> panel and want to save that image into my desktop. What do I need to add? ``` JFileChooser chooser = new JFileChooser(); chooser.showOpenDialog(panel); ``` I've added the following code to my paintComponent method: ``` bi = new BufferedImage(panel.getSize().width,panel.getSize().height, BufferedImage.TYPE_INT_ARGB); g = bi.createGraphics(); ``` And then the Save Button does this... What else am I missing? Or doing incorrectly more so. ``` JFileChooser chooser = new JFileChooser(); chooser.showSaveDialog(panel); try{ImageIO.write(bi,"png",new File("test.png"));}catch (Exception ex) {} ```