title: Display to GUI and Save to Disk with a Single Object/Variable
body: <p>I'm struggling a bit trying to understand how to effectively use java image objects.</p>
  
  <p>I have a very simple program which draws an image and then saves that image to the disk.</p>
  
  <pre><code>public class myBrain {
  
     public static void main(String[] args) {
  
        JFrame lv_frame = new JFrame();
        lv_frame.setTitle("Drawing");
        lv_frame.setSize(300, 300);
        lv_frame.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
  
        lv_frame.add(new image());
  
        lv_frame.setVisible(true);
  
     }
  
  }
  
  class image extends JPanel {
  
     public void paintComponent(Graphics graphic) {
  
        super.paintComponent(graphic);
  
        // draw image to gui
  
        Graphics2D graphic2D = (Graphics2D) graphic;
        graphic2D.fillArc(0, 0, 250, 250, 0, 90);
  
        // save image to disk
  
        BufferedImage image = new BufferedImage(250, 250, BufferedImage.TYPE_4BYTE_ABGR_PRE);
  
        graphic2D = image.createGraphics();
        graphic2D.fillArc(0, 0, 250, 250, 0, 90);
  
        try {
           File output = new File("file.png");
           ImageIO.write(image, "png", output);
        } catch(IOException log) {
           System.out.println(log);
        }
  
     }
  
  }
  </code></pre>
  
  <p>In the code there are two functions (I have written them in a single function for ease of understanding purposes). One to handle the gui and one to handle the saving.</p>
  
  <p>The first one has it's Graphics2D object which it draws the arc to which is fine.</p>
  
  <p>The second one needs a BufferedImage so that I can save it however when I try to create the graphic for the BufferedImage (image.createGraphics();) it overrides what I have drawn previously on the graphic2D which means I have to then draw it again (fillArc(0, 0, 250, 250, 0, 90);).</p>
  
  <p>Is there a way around this so that I don't need to draw the image a second time to save it?</p>