4

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) {}
James Brown
  • 919
  • 3
  • 13
  • 22
  • 1
    Take a look at [this](http://stackoverflow.com/questions/12984207/cannot-convert-current-canvas-data-into-image-in-java/12984332#12984332) and let me know if it's the same problem – MadProgrammer Oct 23 '12 at 03:42
  • @MadProgrammer Now *that* was an inspired answer that deserves more than the "me & some other 'guy'" votes it got. – Andrew Thompson Oct 23 '12 at 04:55

1 Answers1

6

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I'm just drawing polygons onto a class that extends JPanel. I create them via the paintComponent method. I have a save button that will run the code above. – James Brown Oct 23 '12 at 02:57
  • 1
    @James: please the re-iteration of my recommendation in the edit to my answer. – Hovercraft Full Of Eels Oct 23 '12 at 03:00
  • 2
    *"the smart way would be to draw into a BufferedImage, and then simply save the BufferedImage"* See [this answer](http://stackoverflow.com/questions/12683533/drawing-a-rectangle-that-wont-disappear-in-next-paint/12683632#12683632) for an example. Alternately to gain a snapshot of the panel (less preferable) see [this answer](http://stackoverflow.com/questions/5853879/java-swing-obtain-image-of-jframe/5853992#5853992). – Andrew Thompson Oct 23 '12 at 03:10
  • I've updated what I have so far, hopefully I am on the right track, but still am missing something. – James Brown Oct 23 '12 at 03:13
  • 1
    *"still am missing something. "* **Start with** changing.. `catch (Exception ex) {}` to `catch (Exception ex) { ex.printStackTrace(); }` – Andrew Thompson Oct 23 '12 at 03:25