Java 2D Graphics public void append(Printable painter, PageFormat
Java 2D Graphics public void append(Printable painter, PageFormat page) This method adds a single page to the end of this Book. The page will have the given PageFormat and be rendered using the supplied Printable. public void append(Printable painter, PageFormat page, int numPages) This method adds numPages new pages to the end of this Book. Each of the new pages will use the supplied Printable and PageFormat. public void setPage(int pageIndex, Printable painter, PageFormat page) Use this method to change an existing page in this Book. The page will use the supplied Printable and PageFormat. The following example shows how to use a Book. It uses the Patchwork-Component from earlier in this chapter. Two of these components are created and wrapped with BookComponentPrintable instances. The BookComponent-Printable class is just like the ComponentPrintable presented earlier, with one important difference. While the ComponentPrintable class was only designed to print the first page of a print job, BookComponentPrintable is capable of printing any page. Thus, its print() method never returns NO_SUCH_PAGE. The other important thing to notice is that instead of calling setPrintable() on the PrinterJob, this example creates a Book and passes it to the setPageable() method. When you run this example, you should get two pages of output. The first page is in portrait orientation and contains the string “printable1,” while the second page is in landscape orientation with the string “printable2.” import java.awt.*; import java.awt.print.*; public class Booker { public static void main(String[] args) { PrinterJob pj = PrinterJob.getPrinterJob(); // Create two Printables. Component c1 = new PatchworkComponent(”printable1″); Component c2 = new PatchworkComponent(”printable2″); c1.setSize(500, 400); c2.setSize(500, 400); BookComponentPrintable printable1 = new BookComponentPrintable(c1); BookComponentPrintable printable2 = new BookComponentPrintable(c2); // Create two PageFormats. PageFormat pageFormat1 = pj.defaultPage(); PageFormat pageFormat2 = (PageFormat)pageFormat1.clone(); pageFormat2.setOrientation(PageFormat.LANDSCAPE); // Create a Book. Book book = new Book(); book.append(printable1, pageFormat1); book.append(printable2, pageFormat2); // Print the Book. pj.setPageable(book); if (pj.printDialog()) { try { pj.print(); } catch (PrinterException e) { System.out.println(e); } page 252
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services