Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/dinopolis/util/PrintUtilities.java


1   package org.dinopolis.util;
2   
3   import java.awt.Component;
4   import java.awt.Graphics2D;
5   import java.awt.Graphics;
6   import java.awt.print.PageFormat;
7   import java.awt.print.Printable;
8   import java.awt.print.PrinterException;
9   import java.awt.print.PrinterJob;
10  import javax.swing.RepaintManager;
11  
12  
13  
14  
15  /** A simple utility class that lets you very simply print
16   *  an arbitrary component. Just pass the component to the
17   *  PrintUtilities.printComponent. The component you want to
18   *  print doesn't need a print method and doesn't have to
19   *  implement any interface or do anything special at all.
20   *  <P>
21   *  If you are going to be printing many times, it is marginally more 
22   *  efficient to first do the following:
23   *  <PRE>
24   *    PrintUtilities printHelper = new PrintUtilities(theComponent);
25   *  </PRE>
26   *  then later do printHelper.print(). But this is a very tiny
27   *  difference, so in most cases just do the simpler
28   *  PrintUtilities.printComponent(componentToBePrinted).
29   *
30   *  7/99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
31   *  May be freely used or adapted.
32   */
33  
34  public class PrintUtilities implements Printable
35  {
36    protected Component componentToBePrinted;
37  
38    /**
39     * Prints the given component.
40     *
41     * @param componentToBePrinted the component that should be printed.
42     */
43    public static void printComponent(Component componentToBePrinted)
44    {
45      new PrintUtilities(componentToBePrinted).print();
46    }
47    
48    
49    /**
50     * Constructor taking the component to be printed
51     *
52     * @param componentToBePrinted the component that should be printed.
53     */
54    PrintUtilities(Component componentToBePrinted)
55    {
56      this.componentToBePrinted = componentToBePrinted;
57    }
58    
59    /**
60     * Prints the component set in the constructor
61     */
62    public void print()
63    {
64      PrinterJob printJob = PrinterJob.getPrinterJob();
65      printJob.setPrintable(this);
66      if (printJob.printDialog())
67        try
68        {
69          printJob.print();
70        } catch(PrinterException pe)
71        {
72          System.out.println("Error printing: " + pe);
73        }
74    }
75  
76    /**
77     * Implementation of the Printable interface.
78     * 
79     * @param g
80     * @param pageFormat
81     * @param pageIndex
82     * @return result code.
83     * @see java.awt.print.Printable
84     */
85    public int print(Graphics g, PageFormat pageFormat, int pageIndex)
86    {
87      if (pageIndex > 0)
88      {
89        return(NO_SUCH_PAGE);
90      }
91      else
92      {
93        Graphics2D g2d = (Graphics2D)g;
94        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
95        disableDoubleBuffering(componentToBePrinted);
96        componentToBePrinted.paint(g2d);
97        enableDoubleBuffering(componentToBePrinted);
98        return(PAGE_EXISTS);
99      }
100   }
101 
102   /** The speed and quality of printing suffers dramatically if
103    *  any of the containers have double buffering turned on.
104    *  So this turns if off globally.
105    *  @see #enableDoubleBuffering(Component)
106    */
107   public static void disableDoubleBuffering(Component c)
108   {
109     RepaintManager currentManager = RepaintManager.currentManager(c);
110     currentManager.setDoubleBufferingEnabled(false);
111   }
112 
113   /** 
114    * Re-enables double buffering globally. 
115    */
116   
117   public static void enableDoubleBuffering(Component c)
118   {
119     RepaintManager currentManager = RepaintManager.currentManager(c);
120     currentManager.setDoubleBufferingEnabled(true);
121   }
122 }