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

Quick Search    Search Deep

Source code: com/paradoxpoint/libitina/util/PrintUtilities.java


1   /* 
2    * Libitina - Funeral Monument Image Compositor
3    * Copyright (C) 2003,2004  Luke Imhoff (see class description for original notice)
4    *
5    * Contact Info:
6    * luke@paradoxpoint.com
7    * Luke Imhoff
8    * 2514 Pied Piper Lane
9    * Wausau, WI 54403 
10   *
11   * This program is free software; you can redistribute it and/or
12   * modify it under the terms of the GNU General Public License
13   * as published by the Free Software Foundation; either version 2
14   * of the License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   *
21   * You should have received a copy of the GNU General Public License
22   * along with this program; if not, write to the Free Software
23   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24   */
25  package com.paradoxpoint.libitina.util;
26  
27  import java.awt.*;
28  import javax.swing.*;
29  import java.awt.print.*;
30  
31  /** A simple utility class that lets you very simply print
32   *  an arbitrary component. Just pass the component to the
33   *  PrintUtilities.printComponent. The component you want to
34   *  print doesn't need a print method and doesn't have to
35   *  implement any interface or do anything special at all.
36   *  <P>
37   *  If you are going to be printing many times, it is marginally more 
38   *  efficient to first do the following:
39   *  <PRE>
40   *    PrintUtilities printHelper = new PrintUtilities(theComponent);
41   *  </PRE>
42   *  then later do printHelper.print(). But this is a very tiny
43   *  difference, so in most cases just do the simpler
44   *  PrintUtilities.printComponent(componentToBePrinted).
45   *
46   *  7/99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
47   *  May be freely used or adapted.
48   *
49   *  Luke Imhoff - added scale-to-page support
50   *              - changed error message to use System.err
51   *  @author Marty Hall
52   *  @author Luke Imhoff
53   */
54  
55  public class PrintUtilities implements Printable {
56    private Component componentToBePrinted;
57  
58    public static void printComponent(Component c) {
59      new PrintUtilities(c).print();
60    }
61    
62    public PrintUtilities(Component componentToBePrinted) {
63      this.componentToBePrinted = componentToBePrinted;
64    }
65    
66    public void print() {
67      PrinterJob printJob = PrinterJob.getPrinterJob();
68      printJob.setPrintable(this);
69      if (printJob.printDialog())
70        try {
71          printJob.print();
72        } catch(PrinterException pe) {
73          System.err.println("Error printing: " + pe);
74        }
75    }
76  
77    public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
78      if (pageIndex > 0) {
79          return(NO_SUCH_PAGE);
80      }
81      // first compute the scale factor of the component
82      int w=componentToBePrinted.getWidth();
83      int h=componentToBePrinted.getHeight();
84      
85      // change w and h because if we rotate ge GC
86      double sx=pageFormat.getImageableWidth()/(double)w;
87      double sy=pageFormat.getImageableHeight()/(double)h;
88      double sf = Math.min(sx,sy);
89      Graphics2D g2d = (Graphics2D)g;
90      g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
91      g2d.scale(sf,sf);
92      disableDoubleBuffering(componentToBePrinted);
93      componentToBePrinted.paint(g2d); 
94      enableDoubleBuffering(componentToBePrinted);
95      return(PAGE_EXISTS);
96    }
97    
98    /** The speed and quality of printing suffers dramatically if
99     *  any of the containers have double buffering turned on.
100    *  So this turns if off globally.
101    *  @see enableDoubleBuffering
102    */
103   public static void disableDoubleBuffering(Component c) {
104     RepaintManager currentManager = RepaintManager.currentManager(c);
105     currentManager.setDoubleBufferingEnabled(false);
106   }
107 
108   /** Re-enables double buffering globally. */
109   
110   public static void enableDoubleBuffering(Component c) {
111     RepaintManager currentManager = RepaintManager.currentManager(c);
112     currentManager.setDoubleBufferingEnabled(true);
113   }
114 }