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

Re: Printing to a PNG image



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

Dear Nicolas,

I have wrestled with a similar problem, although it was a while ago
now and I don't fully remember how I solved it!  However, maybe the
following will help:

1) Conversation on this mailing list:
http://www.epic.noaa.gov/java/sgt/mail/msg00383.html.
2) Random extract from my notes:

---- Begin notes ----
This works but draws the plot with borders etc and uses addNotify(),
which isn't really supposed to be called directly.

See http://mail.epic.noaa.gov/sgt/msg00148.html

       BufferedImage im = new BufferedImage(500, 500,
BufferedImage.TYPE_4BYTE_ABGR);

       JGridDemo demo = new JGridDemo();
       JPlotLayout graph = demo.makeGraph();
       graph.getComponent().addNotify();
       graph.getComponent().validate();
       graph.draw(im.createGraphics());
       ImageIO.write(im, "png", new File("C:\\jplot.png"));
---- End notes ----

I'm now working on other things so I probably won't be of much help in
deciphering this if it's not clear to you I'm afraid.  However I'll
help if I can.

Let me know if this works for you!

Cheers, Jon

On 10/10/07, Nicolas Michael <software@nmichael.de> wrote:
> *************************************************
> 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.
>


-- 
--------------------------------------------------------------
Dr Jon Blower              Tel: +44 118 378 5213 (direct line)
Technical Director         Tel: +44 118 378 8741 (ESSC)
Reading e-Science Centre   Fax: +44 118 378 6413
ESSC                       Email: jdb@mail.nerc-essc.ac.uk
University of Reading
3 Earley Gate
Reading RG6 6AL, UK
--------------------------------------------------------------

*************************************************
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.