package org.khelekore.jsizer;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

/** An action that exports a png image of the current graph. */
class PNGAction extends ExportImageAction {
    private static final long serialVersionUID = 1;

    public PNGAction (SizePanel sp, JFrame f) {
	super ("Save png image...", sp, f);
    }

    @Override protected String getFileExtension () {
	return "png";
    }
    
    @Override protected void writeImage (SizePanel sp, File out) 
	throws IOException {
	BufferedImage i = null;
	Graphics g = null;	    
	try {
	    i = new BufferedImage (sp.getWidth (), sp.getHeight (), 
				   BufferedImage.TYPE_INT_ARGB);
	    g = i.getGraphics ();
	    sp.paint (g);
	    ImageIO.write (i, "PNG", out);
	} finally {
	    if (g != null)
		g.dispose ();
	    if (i != null)
		i.flush ();
	}
    }
}
