Java Reference
In-Depth Information
Listing 9-4.
Invoking the Superclass's paint Method
public class PaintingContainer extends Container {
public void paint(Graphics g) {
// paint my contents first…
super.paint(g);
}
}
A full discussion of the
Graphics
class and its methods is beyond the scope of this
chapter, but it's important to remember that a
Graphics
object both stores the context of a
particular set of drawing primitives (font, color, clip region, and so on) and provides meth-
ods to draw graphic primitives, including rectangles, arcs, lines, polygons, characters, and
images. The last primitive—the ability to draw images—may be key for your window
toolkit components, because you can have an artist draw images for various parts of a
component (such as a button's boundary, background, and so forth), and then your
Component
subclass can simply composite those images with the
Graphics
object in its
paint
method with a minimum of primitive graphics to provide a specific look and feel.
Writing a Simple, Lightweight Component
Implementing a rich windowing toolkit
can
be a great deal of work, but it doesn't have to
be, especially if your application only has a few user-interface primitives. Listing 9-5
demonstrates a simple component: a round button bearing a text label.
Listing 9-5.
The RoundButton Lightweight Component
import java.awt.*;
import java.lang.*;
import java.util.*;
import java.awt.event.*;
public class RoundButton extends Component {
protected String label;
protected boolean pressed = false;
private Image offscreen;
private static int PREFERRED_SIZE = 100;
private static int LABEL_PAD = 40;
public RoundButton(String l) {
label = l;
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}