 |
Printing : An example that prints on two pages
The code sample below is a complete Java application. You can
copy and paste it into your Java development application. The diagram and text
beneath the code clarify the interaction between the objects during the print
process.
// Printing Sample code
//
// This code demonstrates the Java 2 print mechanism
import java.awt.*;
import java.awt.print.*;
import java.awt.geom.*;
// Define a class that is called per page that needs printing. It implements
// the one and only method in the Printable interface : print. Note that
// this is quite separate from the PrinterJob class print() method.
//
// This method does not actually do any printing. All it does is write text
// and/or graphics onto the passed page (graphics context). The calling
// printer job object will then pass this page to the printer.
class PrintObject implements Printable
{
public int print (Graphics g, PageFormat f, int pageIndex)
{
Graphics2D g2 = (Graphics2D) g; // Allow use of Java 2 graphics on
// the print pages :
// A rectangle that shows the printable area of the page, allowing
// for margins all round. To be drawn on the first page (index = 0).
Rectangle2D rect = new Rectangle2D.Double(f.getImageableX(),
f.getImageableY(),
f.getImageableWidth(),
f.getImageableHeight());
// A simple circle to go on the second page (index = 1).
Ellipse2D circle = new Ellipse2D.Double(100,100,100,100);
switch (pageIndex)
{
case 0 : g2.setColor(Color.black); // Page 1 : print a rectangle
g2.draw(rect);
return PAGE_EXISTS;
case 1 : g2.setColor(Color.red); // Page 2 : print a circle
g2.draw(circle);
return PAGE_EXISTS;
default: return NO_SUCH_PAGE; // No other pages
}
}
}
public class Sample1
{
public static void main (String[] args)
{
// Create an object that will hold all print parameters, such as
// page size, printer resolution. In addition, it manages the print
// process (job).
PrinterJob job = PrinterJob.getPrinterJob();
// It is first called to tell it what object will print each page.
job.setPrintable(new PrintObject());
// Then it is called to display the standard print options dialog.
if (job.printDialog())
{
// If the user has pressed OK (printDialog returns true), then go
// ahead with the printing. This is started by the simple call to
// the job print() method. When it runs, it calls the page print
// object for page index 0. Then page index 1, 2, and so on
// until NO_SUCH_PAGE is returned.
try { job.print(); }
catch (PrinterException e) { System.out.println(e); }
}
}
}
|
The diagram on the right illustrates the interaction
between the objects in the above code.
- The main code creates a new PrinterJob object. This
is not done using the new operator. Instead, a
so-called static "factory" method, getPrinterJob of the
PrinterJob class is used.
This new object controls the printing.
- When it prints, it does so by asking a print object
to format each page, one at a time. One per call. The setPrintable
method of the job is called to tell it about our print object. We
create an object of our PrintObject class dynamically when we call
this method.
- The standard printer dialog is caled from another
PrinterJob method. This allows the user to define page formats,
margins and so on. If it returns true, the final method of PrinterJob, print(), is called. It takes no parameters. It simply
starts the print process.
- The job then calls the printer object once per page
it wants to print. The printer object builds each page as it is
called.
- The first page is printed with a rectangle lying on
the margin boundary of the page. The second page is printed with a
red circle.
- The printer object tells the job NO_SUCH_PAGE when
it is passed an index to a page it cannot print.
|
 |
| | Copyright© 1998-2004 All Rights Reserved. No portion of this site may be reproduced or redistributed without prior written permission from VistaEdge Technologies
All registered trademarks appearing on this site are the property of their respective owners. Java is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and other countries. This site is not connected to Sun Microsystems, Inc. and is not sponsored by Sun Microsystems, Inc. | |
|  |