20000224: Printing a VisAD display

Thanks to all who responded to my inquiry.  Here are the results:

For a 2D display, you can use the following:

    private void print()
    {
        Toolkit tk = Toolkit.getDefaultToolkit();
        JPanel c = (JPanel) display.getComponent();
        Frame frame = (Frame) c.getTopLevelAncestor();
        if (tk != null)
        {
            PrintJob pj = 
               tk.getPrintJob(frame, "VisAD Display2D", new Properties());
            if (pj != null)
            {
                Graphics pg = pj.getGraphics();
                if (pg != null)
                {
                    try
                    {
                        c.printAll(pg);
                    }
                    finally
                    {
                        pg.dispose();
                    }
                }
                pj.end();
            }
        }
    }

For a 3D display, this does not work.  All you get is the blank JPanel
to print out in a lovely shade of gray.  The following does work:

    private void print()
    {
        Toolkit tk = Toolkit.getDefaultToolkit();
        JPanel c = (JPanel) display.getComponent();
        Frame frame = (Frame) c.getTopLevelAncestor();
        if (tk != null)
        {
            PrintJob pj = 
                tk.getPrintJob(frame, "VisAD Display3D", new Properties());
            JPanel p = new JPanel();
            if (pj != null)
            {
                Graphics pg = pj.getGraphics();
                if (pg != null)
                {
                    BufferedImage image = display.getImage();
                    pg.drawImage(image, 0, 0, p);
                    pg.dispose();
                }
                pj.end();
            }
        }
    }

I'm not sure about the whole use of JPanel p, but drawImage needs
a component to notify.  You probably use the latter version for
2D also.

A couple of notes:

1) For some reason, the printer output does not contain the whole
image.  It seems to be cropped on the left and the top.

2) This does not account for centering, changing background to
white, etc.

3) This is the original method of printing in Java 1.1.  There are
new methods in java.awt.print that probably should be used (see
Sun's Java Tutorial), but I'll look at that later.

Any other information on better ways to do this would surely be
apprectiated.

Don
*************************************************************
Don Murray                               UCAR Unidata Program
dmurray@xxxxxxxxxxxxxxxx                        P.O. Box 3000
(303) 497-8628                              Boulder, CO 80307
*************************************************************
Unidata WWW Server               http://www.unidata.ucar.edu/
McIDAS Demonstration Machine  http://mcdemo.unidata.ucar.edu/
*************************************************************

  • 2000 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the visad archives: