//--------------------------------------------------------------------------- // MyPrint.class - Eric Pence, 1999 // Displays the print dialog and invokes the Painter object's print() method. // The Vector to be printed is formatted in the calling applet. //--------------------------------------------------------------------------- import java.awt.*; import java.awt.print.*; import java.util.*; public class MyPrint { public MyPrint() {} public void printReport(Vector v) { try { PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(new PrintListing(v)); if (job.printDialog() == true) { // Print the job if the user didn't cancel printing job.print(); } } catch (Exception e) { System.out.println("Exception in printReport()"); e.printStackTrace(); } } } //--------------------------------------------------------------------------- // PrintListing.class - Eric Pence, 1999 // Prints a set of pages from the elements in the passed Vector. //--------------------------------------------------------------------------- import java.awt.*; import java.awt.print.*; import java.util.*; class PrintListing implements Printable { private int rememberedPageIndex = -1; private int cl = 0; private Font fontBold = null; private Font fontPlain = null; private int inch = 0; private int line = 0; private boolean rememberedEOF = false; private int y = 0; private int v = 0; private Vector vrs; public PrintListing(Vector vec) { vrs = vec; } public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException { fontPlain = new Font("Courier", Font.PLAIN, 10); fontBold = new Font("Courier", Font.BOLD, 10); String a[] = new String[9]; try { if (pageIndex != rememberedPageIndex) { // First time we've visited this page rememberedPageIndex = pageIndex; if (rememberedEOF == true) return Printable.NO_SUCH_PAGE; // Each page comes from PrinterJob twice, only format once. return Printable.PAGE_EXISTS; } // Calculate and store the width of a column based on the printable area of a page, // and multiply this value by each column position to locate the print output. int columnsPerPage = 80; double imageWidth = pf.getImageableWidth(); cl = (int) (imageWidth / columnsPerPage); // Page units are 1/72 inch. // One line = 12 units. // Printable image area starts at 1 inch from left, 1 inch from top (x=72, y=72) line = 12; inch = 72; y = inch + line ; boolean headingsPrinted = false; boolean pagePrinted = false; if (pageIndex == 0) v = 0; // Print a page. while ((y + (line * 3) < pf.getImageableY() + pf.getImageableHeight())) { if (v >= vrs.size()) { rememberedEOF = true; break; } // Print headings before printing a line. if (headingsPrinted == false) { printHeadings(g); headingsPrinted = true; } a = (String[]) vrs.elementAt(v); g.drawString(a[0], inch, y); g.drawString(a[1], inch + (cl * 15), y); g.drawString(a[2], inch + (cl * 28), y); g.drawString(a[3], inch + (cl * 33), y); g.drawString(a[4], inch + (cl * 46), y); g.drawString(a[5], inch + (cl * 51), y); g.drawString(a[6], inch + (cl * 56), y); g.drawString(a[7], inch + (cl * 61), y); g.drawString(a[8], inch + (cl * 67), y); pagePrinted = true; y += line; v++; } } if (pagePrinted) { y = (line * 60); g.drawString("" + (pageIndex + 1), inch + (cl * 44), y); } return Printable.PAGE_EXISTS; } catch (Exception e) { System.out.println("Exception in print()"); e.printStackTrace(); return Printable.NO_SUCH_PAGE; } } // Print report headings. public void printHeadings(Graphics g) { try { g.setFont(fontBold); g.drawString("My Report Title", inch + (cl * 35), y); y += (line * 2); g.drawString("Column0", inch, y); g.drawString("Column1", inch + (cl * 15), y); g.drawString("Column2", inch + (cl * 28), y); g.drawString("Column3", inch + (cl * 33), y); g.drawString("Column4", inch + (cl * 46), y); g.drawString("Column5", inch + (cl * 51), y); g.drawString("Column6", inch + (cl * 56), y); g.drawString("Column7", inch + (cl * 61), y); g.drawString("Column8", inch + (cl * 67), y); g.setFont(fontPlain); y += (line * 2); } catch (Exception e) { System.out.println("Exception in printHeadings()"); e.printStackTrace(); } } }