Coverage Report - crosswordsage.PrintUtilities
 
Classes in this File Line Coverage Branch Coverage Complexity
PrintUtilities
0%
0/38
0%
0/6
2
 
 1  
 package crosswordsage;
 2  
 
 3  
 import java.awt.*;
 4  
 import javax.swing.*;
 5  
 import java.awt.print.*;
 6  
 
 7  
 /** A simple utility class that lets you very simply print
 8  
  *  an arbitrary component. Just pass the component to the
 9  
  *  PrintUtilities.printComponent. The component you want to
 10  
  *  print doesn't need a print method and doesn't have to
 11  
  *  implement any interface or do anything special at all.
 12  
  *  <P>
 13  
  *  If you are going to be printing many times, it is marginally more
 14  
  *  efficient to first do the following:
 15  
  *  <PRE>
 16  
  *    PrintUtilities printHelper = new PrintUtilities(theComponent);
 17  
  *  </PRE>
 18  
  *  then later do printHelper.print(). But this is a very tiny
 19  
  *  difference, so in most cases just do the simpler
 20  
  *  PrintUtilities.printComponent(componentToBePrinted).
 21  
  *
 22  
  *  7/99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
 23  
  *  May be freely used or adapted.
 24  
  */
 25  
 
 26  
 public class PrintUtilities implements Printable {
 27  
   private Component componentToBePrinted;
 28  
 
 29  
   public static void printComponent(Component c) {
 30  0
     new PrintUtilities(c).print();
 31  0
   }
 32  
 
 33  0
   public PrintUtilities(Component componentToBePrinted) {
 34  0
     this.componentToBePrinted = componentToBePrinted;
 35  0
   }
 36  
 
 37  
   public void print() {
 38  0
     PrinterJob printJob = PrinterJob.getPrinterJob();
 39  0
     printJob.setPrintable(this);
 40  0
     if (printJob.printDialog())
 41  
       try {
 42  0
         printJob.print();
 43  0
       } catch(PrinterException pe) {
 44  0
         System.out.println("Error printing: " + pe);
 45  0
       }
 46  0
   }
 47  
 
 48  
   public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
 49  0
     if (pageIndex > 0) {
 50  0
       return(NO_SUCH_PAGE);
 51  
     } else {
 52  0
       Graphics2D g2d = (Graphics2D)g;
 53  0
       g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
 54  
 
 55  
 
 56  
 
 57  0
       Dimension d = componentToBePrinted.getSize(); //get size of document
 58  0
       double panelWidth = d.width; //width in pixels
 59  0
       double panelHeight = d.height; //height in pixels
 60  0
       double pageHeight = pageFormat.getImageableHeight(); //height of printer page
 61  0
       double pageWidth = pageFormat.getImageableWidth(); //width of printer page
 62  
 
 63  0
       double scale = pageWidth / panelWidth * 0.7;
 64  0
       int totalNumPages = (int) Math.ceil(scale * panelHeight / pageHeight);
 65  
 
 66  
       //make sure not print empty pages
 67  0
       if (pageIndex >= totalNumPages)
 68  
       {
 69  0
           return Printable.NO_SUCH_PAGE;
 70  
       }
 71  
 
 72  
       //shift Graphic to line up with beginning of next page to print
 73  0
       g2d.translate(0f, -pageIndex * pageHeight);
 74  
 
 75  
       //scale the page so the width fits...
 76  0
       g2d.scale(scale, scale);
 77  
 
 78  
 
 79  
 
 80  
 
 81  0
       disableDoubleBuffering(componentToBePrinted);
 82  0
       componentToBePrinted.paint(g2d);
 83  0
       enableDoubleBuffering(componentToBePrinted);
 84  0
       return(PAGE_EXISTS);
 85  
     }
 86  
   }
 87  
 
 88  
   /** The speed and quality of printing suffers dramatically if
 89  
    *  any of the containers have double buffering turned on.
 90  
    *  So this turns if off globally.
 91  
    *  @see enableDoubleBuffering
 92  
    */
 93  
   public static void disableDoubleBuffering(Component c) {
 94  0
     RepaintManager currentManager = RepaintManager.currentManager(c);
 95  0
     currentManager.setDoubleBufferingEnabled(false);
 96  0
   }
 97  
 
 98  
   /** Re-enables double buffering globally. */
 99  
 
 100  
   public static void enableDoubleBuffering(Component c) {
 101  0
     RepaintManager currentManager = RepaintManager.currentManager(c);
 102  0
     currentManager.setDoubleBufferingEnabled(true);
 103  0
   }
 104  
 }