printing : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

printing

In Java, print usually means text display on the video console log such as System.out. Note that System.err and System.out are PrintStreams not PrintWriters. See the File I/O Amanuensis for code samples. However, in general, the term printing usually means creating hardcopy on paper.

There are two kinds of physical printing:

Java’s usual idea of printing is essentially building a virtual screen display and taking a snapshot of it. You build it page by page the same way you build a screen. You don’t emit characters in a stream with embedded control chars the way you did in DOS (Disk Operating System) days.

For the rest of this discussion, I will be mostly referring to the second type printing. Within that, there are three different APIs (Application Programming Interfaces) to choose from.

Programmers were not happy with the AWT (Advanced Windowing Toolkit) print interface and Sun has made been several attempts to replace the API (Application Programming Interface). This has mainly lead to confusion. The key is to get clear on just which printing API you are using and stick to it.

The Java 1.0 JDK (Java Development Kit) does not support printing.Java version 1.1 does, via java.awt.PrintJob. You print using the same techniques you use for drawing on the screen. It is much like drawing a high-res screen the size of a sheet of paper. Components now have a print method to print themselves which is usually the same as the one to draw them on the screen. To handle text you use Canvases and drawString, or TextFields. To get properly aligned text without borders, consider the KL-Group components or Swing.

In Java version 1.1 unsigned Applets could not print. In more recent versions, they can, though the user has to give an ok.

How To Use PrintJob

For printing on old JVMs (Java Virtual Machines):

In W95, W98, Me and NT, the print job properties are ignored. They are system dependent. But here is how Sun implements them:

How To Use PrinterJob

Java version 1.2 or later PrinterJob is similar to PrintJob. It pops up a platform specific dialog box that the end user fills into select options.

I don’t know how you go about setting attributes that the end user would otherwise have to select in the dialog.

Attributes

Oracle’s Javadoc on Printing Attributes package : available:

Designing Printouts

Here is a little PostScript grid.ps program.
copy grid.ps lpt1:
(where lpt1: is a PostScript printer) to print out a 1/10 by 1/6 grid on transparent film. It will be useful in designing or checking printouts, especially those on pre-printed forms.

Faster Printing with javax.print

Dot matrix printing is especially slow. To be quick, you must send text intermixed with printer-specific formatting commands. You can do that with RTextPrinter (not part of standard Java). The following hunk of code presumes you have already, through some other process, created a file file.ps full of PostScript printer commands and you want to get it printed on a printer that can print five double-sided copies on size A4 paper.

Of course, you can always emit your printer codes to a file, then exec a copy to LPTn: command or whatever your specific platform requires. For a network-capable printer you can talk directly to it via a TCP/IP (Transmission Control Protocol/Internet Protocol) socket.

Learning More

Oracle’s Javadoc on java.awt.PrintJob class : available:
Oracle’s Javadoc on java.awt.print.PrinterJob class : available:
Oracle’s Javadoc on javax.print package : available:
Oracle’s Javadoc on javax.print.attribute package : available:
Oracle’s Javadoc on PrintRequestAttributeSet class : available:
Oracle’s Javadoc on HashPrintRequestAttributeSet class : available:
Oracle’s Javadoc on javax.print.attribute.standard package : available:

This page is posted
on the web at:

http://mindprod.com/jgloss/printing.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\printing.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[44.202.128.177]
You are visitor number