Graphics Before Java 1.2

Prior to the introduction of the Java 2D API in Java 1.2, the java.awt.Graphics class provided only rudimentary graphics capabilities. It allowed you to draw lines, draw and fill simple shapes, display text, and draw images and perform basic image manipulation. You could specify a drawing color, a font, a clipping region, and the location of the origin. Notably missing, however, was the ability to specify a line width or rotate or scale drawings. Example 12-1 is a listing of the GraphicsSampler applet that demonstrates all the pre-Java 1.2 basic drawing primitives and attributes. The output of this applet is shown in Figure 12-1.

A sampling of graphics primitives

Figure 12-1. A sampling of graphics primitives

If you have read Chapter 16, you should understand the structure of this code. This is a very simple applet, however, so you should be able to understand it even before you read that chapter. The init( ) method is called once, when the applet first starts up, and performs one-time initialization. The paint( ) method displays the content of the applet. The Graphics object passed to this method represents the drawing surface of the applet; it allows you to draw anywhere within the bounds of the Applet object. tile( ) and centerText( ) are utility methods defined for the convenience of the paint( ) method. They demonstrate important graphics drawing techniques, but have nothing to do ...

Get Java Examples in a Nutshell, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.