-2

I'm struggling a bit trying to understand how to effectively use java image objects.

I have a very simple program which draws an image and then saves that image to the disk.

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);
      }

   }

}

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.

The first one has it's Graphics2D object which it draws the arc to which is fine.

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);).

Is there a way around this so that I don't need to draw the image a second time to save it?

TheLovelySausage
  • 3,838
  • 15
  • 56
  • 106
  • 2
    Not the cause of your problem, but never do file I/O from within any component's painting method (such as paintComponent), ever. This method should be for painting the component and painting the component only. Also don't forget to call the super method. – Hovercraft Full Of Eels Feb 29 '16 at 15:10
  • I know, I will be removing it from the painting method once I have a working version. Leaving off the super method was an error I'll add it back in – TheLovelySausage Feb 29 '16 at 15:24
  • 1
    so you are doing the same question over and over again every few hours...http://stackoverflow.com/questions/35696758/java-passing-2d-graphic-as-a-parameter/35697773#35697773 – gpasch Feb 29 '16 at 15:34
  • @gpasch That question wasn't very clear, I'm not sure how to close it though. This is a revised version to better explain the problem – TheLovelySausage Feb 29 '16 at 15:35
  • Then you should improve the original question, not re-ask the same or similar question over again. Doing this can waste people's time. For all we know we could be re-stating that which has been stated before. – Hovercraft Full Of Eels Feb 29 '16 at 16:14

1 Answers1

1

You should start painting on your BufferedImage, retain it (as a class member for example), then in paintComponent method draw that BufferedImage (graphics2D.drawImage(...)).

The saving method should save the same BufferedImage.

Xvolks
  • 2,065
  • 1
  • 21
  • 32
  • This sounds like it could solve my problems although I'm unclear on how to implement it, would you be able to update the code in my question to reflect your suggestion? – TheLovelySausage Feb 29 '16 at 15:27