home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Book Home Java Enterprise in a Nutshell Search this book

Chapter 5. Printing

The previous chapters of this book have described how to draw graphics and display graphical user interfaces on a computer screen. This chapter explains how to transfer those graphics to hardcopy. Printing was not supported in Java 1.0. Java 1.1 added a simple printing API that was easy to use but was not tightly integrated with the printing capabilities of the underlying operating system. The Java 2 platform introduces an entirely new printing API that addresses the shortcomings of the Java 1.1 API. This chapter explains both the Java 1.1 and the Java 1.2 APIs.

5.1. Printing in Java 1.1

In Java 1.1, you use a Graphics object to draw to the screen or into an off-screen Image. To produce hardcopy, you do exactly the same thing: obtain a Graphics object that represents your printer and use the methods of that object to draw to the printer. The only tricky thing you need to know is how to obtain an appropriate Graphics object. You do this with a java.awt.PrintJob object, which you can obtain from the Toolkit object.

The basic Java 1.1 printing algorithm has the following steps:

  1. First, you must begin the print job. You do this by calling the getPrintJob() method of the Toolkit object. This method displays a dialog box to the user to request information about the print job, such as the name of the printer it should be sent to. getPrintJob() returns a PrintJob object.

  2. To begin printing a page, you call the getGraphics() method of the PrintJob object. This returns a Graphics object that implements the PrintGraphics interface, to distinguish it from an on-screen Graphics object.

  3. Now you can use the various methods of the Graphics object to draw your desired output on the page. If you are printing an applet or a custom AWT component, you can simply pass your Graphics object to the paint() method of the applet or component. Note, however, that built-in AWT components are drawn by the native GUI system, rather than a paint() method, and may not print correctly.

  4. When you are done drawing the page, you call the dispose() method of the Graphics object to send that page description to the printer. If you need to print another page, you can call the getGraphics() method of the PrintJob again to obtain a new Graphics object for the next page and repeat the process of drawing and calling dispose().

  5. When you have printed all of your pages, you end the print job itself by calling the end() method of the PrintJob object.

With the Java 1.1 printing API, the coordinate system of the printer is very much like the coordinate system used when drawing on-screen. The origin is at the top left, X coordinates run from left to right, and Y coordinates run from the top to the bottom of the page. The coordinate system uses a resolution of 72 points per inch, which is a typical resolution for monitors as well. Most printers support much higher resolutions than this, however, and they use that extra resolution when printing text, for example. However, because the Java 1.1 Graphics object does not allow floating-point coordinates, all graphics must be positioned exactly at integer positions.

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.