Toolkit and Peers

Contents:
Toolkit
The Peer Interfaces

This chapter describes the Toolkit class and the purposes it serves. It also describes the java.awt.peer package of interfaces, along with how they fit in with the general scheme of things. The most important advice I can give you about the peer interfaces is not to worry about them. Unless you are porting Java to another platform, creating your own Toolkit, or adding any native component, you can ignore the peer interfaces.

Toolkit

The Toolkit object is an abstract class that provides an interface to platform-specific details like window size, available fonts, and printing. Every platform that supports Java must provide a concrete class that extends the Toolkit class. The Sun JDK provides a Toolkit for Windows/95 (sun.awt.win32.MToolkit [Java1.0] or sun.awt.windows.MToolkit [Java1.1]), Solaris/Motif (sun.awt.motif.MToolkit), and Macintosh (sun.awt.macos.MToolkit). Although the Toolkit is used frequently, both directly and behind the scenes, you would never create any of these objects directly. When you need a Toolkit, you ask for it with the static method getDefaultToolkit() or the Component.getToolkit() method.

You might use the Toolkit object if you need to fetch an image in an application (getImage()), get the font information provided with the Toolkit (getFontList() or getFontMetrics()), get the color model (getColorModel()), get the screen metrics (getScreenResolution() or getScreenSize()), get the system clipboard (getSystemClipboard()), get a print job (getPrintJob()), or ring the bell (beep()). The other methods of Toolkit are called for you by the system.

Toolkit Methods

Constructors Pseudo-Constructors System information
UNIX Printing Properties
Property Name Meaning and Possible Values
awt.print.printer The name of the printer on your system to send the job to.
awt.print.fileName The name of the file to save the print job to.
awt.print.numCopies The number of copies to be printed.
awt.print.options Other options to be used for the run-time system's print command.
awt.print.destination Whether the print job should be sent to a printer or saved in a file.
awt.print.paperSize The size of the paper on which you want to print--usually, letter.
awt.print.orientation Whether the job should be printed in portrait or landscape orientation.
Images

The Toolkit provides a set of basic methods for working with images. These methods are similar to methods in the Applet class; Toolkit provides its own implementation for use by programs that don't have access to an AppletContext (i.e., applications or applets that are run as applications). Remember that you need an instance of Toolkit before you can call these methods; for example, to get an image, you might call Toolkit.getDefaultToolkit().getImage(`myImage.gif`).

NOTE:

This version of getImage() is not usable within browsers since it will throw a security exception because the applet is trying to access the local filesystem.

NOTE:

For those unfamiliar with magic numbers, most data files are uniquely identified by the first handful or so of bytes. For instance, the first three bytes of a GIF file are "GIF". This is what createImage() relies upon to do its magic.Miscellaneous methods