How to save a Graphics2D plot into a file ?

I have a piece of JAVA software that generates a 2D plot based on Graphics2D and displays it on the screen.I would like to know how to save the 2D plot into a file with one of the following formats.BMP, Postscript, PF,PNG.Thanks
[256 byte] By [RALIIYAMAa] at [2007-11-15]
# 1

Something like off the top of my head

int width=100, height=100;

BufferedImage bi = new BufferedImage(width, height, BufferedImage.SCALE_SMOOTH);

Graphics2D g2d = bi.createGraphics();

// Manipulate your g2d graphic

g2d.dispose();

ImageIO.write(bim, "png", new File("out.png"));