[Date Prev][Date Next][Index][Thread] [SGT Mail Archive] [SGT Main Page]

Printing to a PNG image



*************************************************
Message from the sgt mail list.
*************************************************

Hello,

I'm using SGT for displaying graphs, which works fine in my GUI application.
Also printing the graph that is displayed on the screen into a PNG image works
fine. 

However, I'm trying to also create graphs in an command-line mode (not
displaying them within a JFrame) and directly writing them to a PNG image. 
This
always results in an empty (all white) image.

This is basically the code I use:

public class GraphPanel extends gov.noaa.pmel.sgt.JPane {
    ....

    public void updateGraphPanel() {
        ... layers and graphs created inside this method ...
    }

    public void saveImageToFile(String fname) {
        int width = this.getWidth();
        int height = this.getHeight();
        
        BufferedImage img = new
BufferedImage(width,height,BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D g = (Graphics2D)img.getGraphics();
        try {
            g.setBackground(Color.WHITE);
            this.paintComponent(g);
            File file = new File(fname);
            javax.imageio.ImageIO.write(img, "PNG", file);
        } catch (Exception ee) {
            ee.printStackTrace();
        } finally {
            g.dispose();
        }
    }
}

GraphPanel graphPanel = new GraphPanel(project,1024,768); // create new
GraphPanel with size 1024x768
graphPanel.updateGraphPanel(); // create all layers and graphs
graphPanel.saveImageToFile("test.png");

I only get a 1024x768 pixel white png image. I already played around with
setBatch(), doLayout(), draw() etc. but couldn't find the right solution.

Any help appreciated!

Nick.


*************************************************
To remove yourself from this mailing list,
send mail to <epic-majordomo@noaa.gov> with
"unsubscribe sgt" in the message body.


Privacy Notice, External Links Disclaimer.