00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 package ikayaki.util;
00024
00025 import javax.swing.*;
00026 import java.awt.*;
00027 import java.awt.print.PageFormat;
00028 import java.awt.print.Printable;
00029 import java.awt.print.PrinterException;
00030 import java.awt.print.PrinterJob;
00031
00037 public class ComponentPrinter implements Printable {
00038
00042 private Component componentToBePrinted;
00043
00047 private final int plotHeight = 200;
00048
00049
00055 public ComponentPrinter(Component componentToBePrinted) {
00056 this.componentToBePrinted = componentToBePrinted;
00057 }
00058
00064 public static void printComponent(Component c) {
00065 new ComponentPrinter(c).print(null);
00066 }
00067
00074 public static void printComponent(Component c, String jobName) {
00075 new ComponentPrinter(c).print(jobName);
00076 }
00077
00081 public void print(String jobName) {
00082 if (jobName == null) {
00083 jobName = "Java Printing";
00084 }
00085 PrinterJob printJob = PrinterJob.getPrinterJob();
00086 printJob.setPrintable(this);
00087 System.out.println(printJob.getJobName());
00088 printJob.setJobName(jobName);
00089 if (printJob.printDialog()) {
00090 try {
00091 printJob.print();
00092 } catch (PrinterException pe) {
00093 System.out.println("Error printing: " + pe);
00094 }
00095 }
00096 }
00097
00108 public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
00109 Dimension dim = componentToBePrinted.getSize();
00110 if (dim.getHeight() < (pageIndex * 695)) {
00111 return (NO_SUCH_PAGE);
00112 }
00113 Graphics2D g2d = (Graphics2D) g;
00114 if (dim.getHeight() - (695 * (pageIndex)) < plotHeight) {
00115 g2d.translate(pageFormat.getImageableX(),
00116 pageFormat.getImageableY() - 695 * pageIndex + (plotHeight - (dim.getHeight() - (695 * (pageIndex)))));
00117 } else {
00118 g2d.translate(pageFormat.getImageableX(),
00119 pageFormat.getImageableY() - 695 * pageIndex);
00120 }
00121 disableDoubleBuffering(componentToBePrinted);
00122 componentToBePrinted.paint(g2d);
00123 if (dim.getHeight() - (695 * (pageIndex + 1)) < plotHeight && dim.getHeight() - (695 * (pageIndex + 1)) > 0) {
00124 System.err.println("lets print page " + (pageIndex + 1));
00125 g2d.setColor(Color.white);
00126 g2d.fillRect(0, 695 * (pageIndex + 1) - (plotHeight - (int) dim.getHeight() + (695 * (pageIndex + 1))),
00127 500, 700);
00128 }
00129 enableDoubleBuffering(componentToBePrinted);
00130 return (PAGE_EXISTS);
00131 }
00132
00133
00134 public static void disableDoubleBuffering(Component c) {
00135 RepaintManager currentManager = RepaintManager.currentManager(c);
00136 currentManager.setDoubleBufferingEnabled(false);
00137 }
00138
00139 public static void enableDoubleBuffering(Component c) {
00140 RepaintManager currentManager = RepaintManager.currentManager(c);
00141 currentManager.setDoubleBufferingEnabled(true);
00142 }
00143 }