Printable Component : Print « 2D Graphics GUI « Java

Printable Component


 * This example is from the book "Java Foundation Classes in a Nutshell".
 * Written by David Flanagan. Copyright (c) 1999 by O'Reilly & Associates.  
 * You may distribute this source code for non-commercial purposes only.
 * You may study, modify, and use this example for any purpose, as long as
 * this notice is retained.  Note that this example is provided "as is",
 * WITHOUT WARRANTY of any kind either expressed or implied.

import java.awt.*;
import java.awt.print.*;

 * This wrapper class encapsulates a Component and allows it to be printed
 * using the Java 2 printing API.
public class PrintableComponent implements Printable {
  // The component to be printed.
  Component c;

  /** Create a PrintableComponent wrapper around a Component */
  public PrintableComponent(Component c) { this.c = c; }

   * This method is not part of the Printable interface.  It is a method
   * that sets up the PrinterJob and initiates the printing.
  public void print() throws PrinterException {
    // Get the PrinterJob object
    PrinterJob job = PrinterJob.getPrinterJob();
    // Get the default page format, then allow the user to modify it
    PageFormat format = job.pageDialog(job.defaultPage());
    // Tell the PrinterJob what to print
    job.setPrintable(this, format);
    // Ask the user to confirm, and then begin the printing process
    if (job.printDialog()) 

   * This is the "callback" method that the PrinterJob will invoke.
   * This method is defined by the Printable interface.
  public int print(Graphics g, PageFormat format, int pagenum) {
    // The PrinterJob will keep trying to print pages until we return
    // this value to tell it that it has reached the end.
    if (pagenum > 0) 
      return Printable.NO_SUCH_PAGE;

    // We're passed a Graphics object, but it can always be cast to Graphics2D
    Graphics2D g2 = (Graphics2D) g;

    // Use the top and left margins specified in the PageFormat Note
    // that the PageFormat methods are poorly named.  They specify
    // margins, not the actual imageable area of the printer.
    g2.translate(format.getImageableX(), format.getImageableY());

    // Tell the component to draw itself to the printer by passing in 
    // the Graphics2D object.  This will not work well if the component
    // has double-buffering enabled.

    // Return this constant to tell the PrinterJob that we printed the page.
    return Printable.PAGE_EXISTS;


Related examples in the same category

1.The Printing code which implements Printable
2.Print an Image to print directly
3.Simplest SWT Print ExampleSimplest SWT Print Example
4.Print in Java 2: PrinterJob
5.Print in Java: page format and document
6.Print in Java: Multi page
7.Print in Java 5
8.Print in Java 6
9.Simple Book for printingSimple Book for printing
10.Shapes PrintShapes Print
11.Display the print dialog and print
12.Print the printable area outlinePrint the printable area outline
13.Print the text file and print preview themPrint the text file and print preview them
14.Printable demoPrintable demo
15.Print Swing componentsPrint Swing components
17.Another print demoAnother print demo
18.Book demoBook demo
19.Printing the Combined-Java 1.2-and-1.4 WayPrinting the Combined-Java 1.2-and-1.4 Way
20.Printing the Java 1.4 Way
21.Prompting for a Printer
22.Printing the Java 1.1 WayPrinting the Java 1.1 Way
24.Printable Document
25.PrintFile -- Print a file named on the command linePrintFile -- Print a file named on the command line
26.Print to the standard output
27.PrintPanel is the base for an open-ended series of classesPrintPanel is the base for an open-ended series of classes
28.Pageable TextPageable Text
29.The area of the printable area
30.The area of the actual page
31.Printing Pages with Different Formats
32.Setting the Orientation of a Printed Page
33.Print Dialog: change the default printer settings(default printer, number of copies, range of pages)
34.Printing to a File
35.Listening for Print Service Status Changes
36.Print Image
37.Overriding the Default Action of a JTextComponent
38.Displaying the Page Format Dialog: changes the default page format such as orientation and paper size.
39.Create PageFormats on a higher level
40.Printing of a multi-page bookPrinting of a multi-page book