Adding Printing Capabilities


TEA APIs

See also TEA Editor Installation and Description

Here you find the description of how to add Printing Capabilities to your applications. The code exposed is taken from the Source Codes of the TETRACTYS Enhanced Apis and is extensively used by the TEA Editor.
Printing in JDK 1.1.1 has some bugs (see Known Problems) but they are only annoying. Our current TEA Editor prints only a page at a time.

Class and Methods

Three are the classes involved in the printing:

  1. Class tea.awt.frame.preview.PrintPreview

    This is a Dialog that shows a preview of the print and can also print the page shown.

  2. Class java.awt.PrintJob

    An abstract class which initiates and executes a print job. It provides access to a print graphics object which renders to an appropriate print device.

  3. Interface java.awt.PrintGraphics

    An inteface which provides a print graphics context for a page.

How to Print Components

This method is taken from the PrintPreview class (this class implements a Preview Dialog that is used by TEA Editor):
public void printDocument() {
  Properties p = null;
  if(parent instanceof MainFrame) {
    p = ((MainFrame)parent).getPropertiesManager().getProperties();
  }
  PrintJob pj = getToolkit().getPrintJob(parent,getTitle(),p);
  if(pj == null) return;
  Graphics pg = pj.getGraphics();
  if (pg != null) {
    previewArea.paintAll(pg);
    pg.dispose();
  }
  pj.end();
}

It's simple to print a page: first of all you retrieve a PrintJob object from the current Toolkit with the method getPrintJob(), then you get a PrintGraphics object (gives a Graphics Context for the print). Finally, you have simply to use the paintAll or printAll method of the component (in this case previewArea). The paintAll method is the same that draws the component on the screen (if some changes are needed simply check for PrintGraphics in the paint method). Remember to dispose the PrintGraphics after the paintAll.

If you have to print multiple pages repeat the procedure for every page.

The Properties passed in the getPrintJob method aren't currently used by the JDK 1.1.1, but it's better to be foreseeing.

The complete source code is available in the TEA APIs page. Write directly to TETRACTYS Freeware for any question or suggestion.


TETRACTYS Freeware Main Page hosted by GeoCities Get your own Free Home Page