home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Java AWT

Previous Chapter 17
Printing
Next
 

17.4 Printing Example

Now that you know about the different classes necessary to print, let's put it all together. Printing takes four steps:

  1. Get the PrintJob:

    PrintJob pjob = getToolkit().getPrintJob(this, "Job Title", (Properties)null);
    

  2. Get the graphics context from the PrintJob:

    Graphics pg = pjob.getGraphics();
    

  3. Print by calling printAll() or print(). When this method returns, you can call dispose() to send the page to the printer:

    printAll(pg);
    pg.dispose(); // This is like sending a form feed
    

  4. Clean up after yourself:

    pjob.end();
    

The following code summarizes how to print:

// Java 1.1 only
PrintJob pjob = getToolkit().getPrintJob(this, "Print?", (Properties)null);
if (pjob != null) {
        Graphics pg = pjob.getGraphics();
        if (pg != null) {
            printAll(pg);
            pg.dispose();
        }
    pjob.end();
}

This code prints the current component: what you get from the printer should be a reasonable rendition of what you see on the screen. Note that we didn't need to modify paint() at all. That should always be the case if you want your printer output to look like your onscreen component.


Previous Home Next
Component Methods Book Index Printing Arbitrary Content

Java in a Nutshell Java Language Reference Java AWT Java Fundamental Classes Exploring Java