20000228: Support for printing added to DisplayImpl

Hi-

A first pass at support for printing the component of a VisAD
Display has been added to DisplayImpl. The updated code is
in the VisAD distribution JAR files. This implementation
uses the java.awt.print classes for printing (PrinterJob
and Printable). See the Java Tutorial section on printing
(http://java.sun.com/docs/books/tutorial/2d/printing) for
more information on using these java.awt.print classes. In
this implementation, the BufferedImage returned from the
DisplayImpl.getImage() method is painted on the PrinterJob graphics
so what you see should be the same as what you would get back from
getImage().


DisplayImp has a new method, getPrintable() which can be used as
follows:

         DisplayImpl display = new DisplayImpl(...);
         << put a bunch of stuff in the display>>
         PrinterJob printJob = PrinterJob.getPrinterJob();
         printJob.setPrintable(display.getPrintable());
         if (printJob.printDialog()) {
             try {
                 printJob.print();
             }
             catch (Exception pe) {
                 pe.printStackTrace();
             }
         }

This will pop up your platform's print controller (ex: Windows Print
Manager) which will allow you to select your printer settings before
printing.

As an added convenience, another class has been added to the visad.util
package. This class, visad.util.PrintActionListener is a subclass of
ActionListener that can be attached to a button or menu (or anything
that has an addActionListener() method). An example of using this would be:

  DisplayImpl di = new DisplayImpl(...);
  ...
  JButton pb = new JButton("Print Me");
  pb.addActionListener(new PrintAction(di));

PrintActionListener basically encapsulates the PrinterJob stuff above in
its actionPerformed() method.

There are a few caveats with this code:

1) There is no support yet for automatically rotating the image or
scaling to fit a page.

2) Prior to JDK1.3, the print() method in Printable is called multiple
times which means it takes a long time for printing to happen. This has
been fixed in JDK 1.3.

3) If there is some other window covering the display's component, that
will become part of the output. This is an artifact of the getImage()
method, and we are looking for a workaround or solution. You can see an
example of this by running Test51 (which uses the getImage method) by
placing another window partially on top of the primary display window.

Any suggestions for improvement would be welcomed. 

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: