I have a JPanel which displays a graph using the Graphic2D. This works fine. I now want to be able to save the graph to a file. So far the only way I can get this to work is create a BufferedImage and everything I write to the JPanels Graphic2D object I write to the Graphic2D object belonging to the BufferedImage and then doing a PrintAll from the BufferedImage. So I have code like the following:
g.setFont(g.getFont().deriveFont(fontSize));
g.drawString(text, xPos, yPos);
g.setFont(saveFont);
bG.setFont(g.getFont().deriveFont(fontSize));
bG.drawString(text, xPos, yPos);
bG.setFont(saveFont);
where g is the Graphic2D object of the JPanel and bG is the Graphic2D object of the BufferedImage
Surely this can't be the best way of doing this. Is there a way of using the Graphic2D object belonging to the JPanel to produce the Graphic2D object for the BufferedImage?