Java 2D Graphics private int mFontSize; private Dimension
Java 2D Graphics public int print(Graphics g, PageFormat pageFormat, int pageIndex) { if (pageIndex >= mPages.size()) return NO_SUCH_PAGE; int savedPage = mCurrentPage; mCurrentPage = pageIndex; Graphics2D g2 = (Graphics2D)g; g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); paint(g2); mCurrentPage = savedPage; return PAGE_EXISTS; } public Dimension getPreferredSize() { return mPreferredSize; } public int getCurrentPage() { return mCurrentPage; } public int getNumPages() { return mPages.size(); } public void nextPage() { if (mCurrentPage < mPages.size() - 1) mCurrentPage++; repaint(); } public void previousPage() { if (mCurrentPage > 0) mCurrentPage–; repaint(); } } The initial release of Java 2 (JDK 1.2) contains printing bugs in Graphics2D’s drawString() methods. Specifically, the draw-String(String, float, float) and drawString(Attributed-CharacterIterator, float, float) methods do not work when printing. These bugs are fixed in JDK 1.2.1. If FilePrinter doesn’t work the first time you try, make sure you’re running JDK 1.2.1. 13.3.3 Advanced Page Control You can use Printable to print as many pages as you want, but they all will have the same page setup. In complex documents, you may want to mix different paper orientations and sizes. To accomplish this, you need the java.awt.print.Pageable interface. This interface specifies a way to associate a PageFormat and a Printable with each page of a print job. Most likely, you will use the java.awt.print.Book class, which implements the Pageable interface. Pageable defines three simple methods: public int getNumberOfPages() This method returns the total number of pages in this Pageable. public PageFormat getPageFormat(int pageIndex) This method returns the PageFormat that corresponds to the given page. public Printable getPrintable(int pageIndex) This method returns the Printable that should be used to render the given page. The Book class supports the three methods described above and adds three more: page 251
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services