Java Reference
In-Depth Information
doesn't allow you to draw directly to the computer screen. Instead, it restricts you
to drawing within the confines of a Component , using a Graphics object that repre-
sents the drawing surface of that component. Thus, to do graphics in Java, you
must have a Component (or a java.applet.Applet ,a Component subclass) to draw
into. Drawing is usually done by subclassing a specific component and defining a
paint() method (or a paintComponent() method, for Swing components). The
examples in this chapter have been structured to focus on the mechanics of draw-
ing, rather than the mechanics of GUI creation, but you will still see some code
that handles GUI-related tasks.
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 11-1 is a listing of the Graphics-
Sampler applet that demonstrates all the pre-Java 1.2 basic drawing primitives and
attributes. The output of this applet is shown in Figure 11-1.
If you have read Chapter 15, Applets , 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 rep-
resents 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 with the applet API.
It is important to understand that the paint() method may be called many times.
The drawing done in this method doesn't necessarily persist: if the applet is cov-
ered by another window and then uncovered, the graphics contained in the applet
may well be lost (whether this actually happens depends on the underlying oper-
ating system and other factors.) In order to restore the graphics, the system
invokes the paint() method again. Thus, all drawing should be done in the
paint() method. If you draw anything in the init() method, those graphics may
be erased and never reappear. (This is one of the serious shortcomings of Example
10-11 and related examples in Chapter 10.)
Example 11•1: GraphicsSampler.java
package com.davidflanagan.examples.graphics;
import java.applet.*;
import java.awt.*;
/**
* An applet that demonstrates most of the graphics primitives in
* java.awt.Graphics.
**/
public class GraphicsSampler extends Applet {
Search WWH ::




Custom Search