Java Reference
In-Depth Information
LISTING 4.6
continued
page.drawArc (BASEX+35, BASEY+40, 15, 10, 180, 180);
// nose
page.drawArc (BASEX+20, BASEY+50, 40, 15, 180, 180);
// mouth
page.setColor (Color.white);
page.drawString ("Always remember that you are unique!",
BASEX-105, BASEY-15);
page.drawString ("Just like everyone else.", BASEX-45, BASEY+105);
}
}
The
paintComponent
method accepts a
Graphics
object as a parameter, which,
as we discussed in Chapter 2, represents the graphics context for a component.
Graphics are drawn on the panel by making method calls to the panel's graphics
context (the
page
parameter).
Every
JPanel
object has a
paintComponent
method that automatically
gets called to draw the panel. In this case we are adding to the definition of
paintComponent
—telling it that in addition to drawing the background of the
panel, it should also draw the face and words as defined by the various calls made
in the
paintComponent
method. The first line of the
paintComponent
method is
a call to
super
.paintComponent
, which represents the regular
JPanel
version of
the
paintComponent
method, which handles the painting of the background. We
will almost always use this as the first line of code in a
paintComponent
method.
Let's look at another example. The
Splat
class shown in Listing 4.7 contains
a
main
method that creates and displays the frame for the program. Visually, this
LISTING 4.7
//********************************************************************
// Splat.java Author: Lewis/Loftus
//
// Demonstrates the use of graphical objects.
//********************************************************************
import
javax.swing.*;
import
java.awt.*;
public class
Splat
{
Search WWH ::
Custom Search