13.1. How Printing Works

The Printing API uses a callback model, which means that the printing system calls your application when it needs something to be drawn. But it's more complicated than that. The following is a typical sequence of events:

  1. The user of your application requests printed output, usually by clicking on a button or choosing a menu item.

  2. Your application displays a print dialog box that is appropriate for the underlying operating system. The Windows print dialog is shown in Figure 13.1. From this dialog, users can choose a printer, change the number of copies that will be printed, and fiddle with other options.

  3. If the user clicks on the OK button in the print dialog, your application can tell the system to begin a printing job.

  4. The system will call back your rendering code at the appropriate time to render your picture on the printer.

Figure 13.1. The print dialog in Windows

13.1.1. The Players

One class and one interface form the crux of the Printing API:

java.awt.print.PrinterJob

The PrinterJob class controls the printing process.[2] You can use this class to kick off printing or to cancel a job in progress. This class also includes methods for displaying a print dialog and a page setup dialog.

[2] Don't confuse this with java.awt.PrintJob, which was part of JDK 1.1's printing solution.

java.awt.print.Printable

The Printable interface represents something that ...

Get Java 2D Graphics 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.