
CHAPTER 5
Pr inting
The previous chapters of this book have described how to draw graphics and dis-
play 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 plat-
for m intr oduces 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.
Pr inting in Java 1.1
In Java 1.1, you use a Graphics object to draw to the screen or into an off-scr een
Image.Toproduce hardcopy, you do exactly the same thing: obtain a Graphics
object that repr esents 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 appropri-
ate
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() retur ns a PrintJob object.
2. To begin ...