Chapter 13. Printing
The Graphics
and
Graphics2D
objects represent a
“drawing surface”; in Chapter 12,
we saw examples of using both the screen and an off-screen buffer as
drawing surfaces. Printing in Java is simply a matter of obtaining a
Graphics
object that uses a printer
as a drawing surface.[1] Once you have a Graphics
object, you can print text and draw
graphics to the printer, just as you do onscreen.
The tricky thing about printing in Java is obtaining the Graphics
object that represents the printer.
The API for doing this keeps changing:
Java 1.1 added the first simple printing API using the
java.awt.PrintJob
class. The major weakness of this API is that it does not support Java 2D graphics.Java 1.2 defined a more advanced printing API in the new package
java.awt.print
. This new API supports Java 2D graphics, includes aPrintable
interface, and provides the ability to explicitly set printing attributes such as page margins, orientation, and use of color. It also allows print jobs to be initiated without displaying a Print dialog to the user.Java 1.3 enhanced the Java 1.1 API by adding the ability to define printing attributes with the
java.awt.JobAttributes
andjava.awt.PageAttributes
classes. Unfortunately, the API is still limited to drawing with the basicGraphics
object, and it cannot use the Java 2D graphics methods defined inGraphics2D
.Java 1.4 defined a new API in the
javax.print
package and subpackages. This new API is interoperable with the Java 1.2 API but ...
Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.