Java 2D Graphics public class FileOpenAction extends AbstractAction
Java 2D Graphics extends AbstractAction { public PageNextPageAction() { super(”Next page”); } public void actionPerformed(ActionEvent ae) { if (mPageRenderer != null) mPageRenderer.nextPage(); showTitle(); } } public class PagePreviousPageAction extends AbstractAction { public PagePreviousPageAction() { super(”Previous page”); } public void actionPerformed(ActionEvent ae) { if (mPageRenderer != null) mPageRenderer.previousPage(); showTitle(); } } } The FilePageRenderer class does the work of rendering each page. It’s a subclass of JComponentthat also implements the Printable interface. The paint() method does all of the page rendering. The print() method adjusts the origin of the Graphics2D to coincide with the paper’s imageable area and then calls paint() to render the page. A FilePageRenderer is created with a File and a PageFormat. It initializes itself in two steps: 1. The entire file is read, line by line. The lines are stored in a Vector member variable, mLines. 2. The FilePageRenderer paginates itself using the supplied PageFormat. It goes through the mLines vector and figures out how many lines will fit on each page. Each page is stored as a Vector of Strings. The pages themselves are stored in a Vector called mPages. This algorithm is contained in the paginate() method. If the current PageFormat changes (i.e., if the user chooses the Page setup menu item), the FilePageRenderer can be repaginated with another call to paginate(). To render a single page, FilePageRenderer simply looks in mPages to find the current page. Then it renders all of the strings for the current page, not really caring whether it’s rendering to the screen or the printer. Here’s an example: import java.awt.*; import java.awt.print.*; import java.io.*; import java.util.Vector; import javax.swing.*; public class FilePageRendererextends JComponentimplements Printable { private int mCurrentPage; // mLines contains all the lines of the file. private Vector mLines; // mPages is a Vector of Vectors. Each of its elements // represents a single page. Each of its elements is // a Vector containing Strings that are the lines for // a particular page. private Vector mPages; private Font mFont; page 249
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services