| 1 | |
package crosswordsage; |
| 2 | |
|
| 3 | |
import java.awt.*; |
| 4 | |
import javax.swing.*; |
| 5 | |
import java.awt.print.*; |
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 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(); |
| 58 | 0 | double panelWidth = d.width; |
| 59 | 0 | double panelHeight = d.height; |
| 60 | 0 | double pageHeight = pageFormat.getImageableHeight(); |
| 61 | 0 | double pageWidth = pageFormat.getImageableWidth(); |
| 62 | |
|
| 63 | 0 | double scale = pageWidth / panelWidth * 0.7; |
| 64 | 0 | int totalNumPages = (int) Math.ceil(scale * panelHeight / pageHeight); |
| 65 | |
|
| 66 | |
|
| 67 | 0 | if (pageIndex >= totalNumPages) |
| 68 | |
{ |
| 69 | 0 | return Printable.NO_SUCH_PAGE; |
| 70 | |
} |
| 71 | |
|
| 72 | |
|
| 73 | 0 | g2d.translate(0f, -pageIndex * pageHeight); |
| 74 | |
|
| 75 | |
|
| 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 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
public static void disableDoubleBuffering(Component c) { |
| 94 | 0 | RepaintManager currentManager = RepaintManager.currentManager(c); |
| 95 | 0 | currentManager.setDoubleBufferingEnabled(false); |
| 96 | 0 | } |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
public static void enableDoubleBuffering(Component c) { |
| 101 | 0 | RepaintManager currentManager = RepaintManager.currentManager(c); |
| 102 | 0 | currentManager.setDoubleBufferingEnabled(true); |
| 103 | 0 | } |
| 104 | |
} |