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:
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.
Copyright © 2001 O'Reilly & Associates. All rights reserved.