Trail: 2D Graphics
|
Lesson: Printing
|
|
Overview of Printing in Java
The system controls the overall printing process, just like it controls
when and how a program can draw. Your application provides information
about the document to be printed, and the printing system determines
when each page needs to be rendered.
This callback printing model enables printing
to be supported on a wide range of printer and systems. It even allows
users to print to a bitmap printer from a computer that doesn't have
enough memory or disk space to hold the bitmap of an entire page. In
this situation the printing system will ask your application to render
the page repeatedly so that it can be printed as a series of smaller
images. (These smaller images are typically referred to as
bands, and this process is commonly called
banded printing.)
To support printing, an application needs to perform two tasks:
- Job control--managing the print job
- Imaging--rendering the pages to be printed
Job Control
Although the system controls the overall printing process, your
application has to get the ball rolling by setting up a
PrinterJob. The PrinterJob , the key point of
control for the printing process, stores the print job properties,
controls the display of print dialogs, and is used to initiate
printing.
To steer the PrinterJob through the printing process, your
application needs to
- Get a
PrinterJob by calling PrinterJob.getPrinterJob
- Tell the
PrinterJob where the rendering code is by
calling setPrintable or setPageable
- If desired, display the Page Setup and Print dialogs by calling
pageDialog and printDialog
- Initiate printing by calling
print
The rendering of pages is controlled by the printing system through
calls to the application's imaging code.
Imaging
Your application must be able to render any page when the printing
system requests it. This rendering code is contained in the
print method of a page
painter--a class that implements the Printable
interface. You implement print to render page contents by
using a Graphics or a Graphics2D rendering
context. You can use either one page painter to render all of the pages
in a print job or different page painters for different types of pages.
When the printing system needs to render a page, it calls
print on the appropriate page painter.
When you use a single page painter, the print job is called a printable job. Using a printable job is the
simplest way to support printing. More complex printing operations that
use multiple page painters are referred to as pageable jobs. In a pageable job an instance of a
class that implements the Pageable interface is used to
manage the page painters.
Printable Jobs
In a printable job all pages use the same page painter and
PageFormat, which defines the size and orientation of the
page to be printed. The page painter is asked to render each page in
indexed order, starting the page at index 0. The page painter might be
asked to render a page multiple times before the next page is
requested, but no pages are skipped. For example, if a user prints
pages 2 and 3 of a document, the page painter is asked to render the
pages at indices 0, 1, and 2 even though the page at index 0 will not
be printed.
If a print dialog is presented, it will not display the number of
pages, because that information is not available to the printing
system. The page painter informs the printing system when the end of
the document is reached.
Pageable Jobs
Pageable jobs are useful if your application builds an explicit
representation of a document, page by page. In a pageable job different
pages can use different page painters and PageFormats. The
printing system can ask the page painters to render pages in any order,
and pages can be skipped. For example, if a user prints pages 2 and 3
of a document, the page painter will be asked to render only the pages
at indices 1 and 2.
The multiple page painters in a pageable job are coordinated by a class
that implements the Pageable interface, such as
Book. A Book represents a collection of pages
that can use different page painters and that can vary in size and
orientation. You can also use your own implementation of the
Pageable interface if Book does not meet your
application's needs.
|