Java Reference
In-Depth Information
Notice that the drawString() and drawImage() methods both have a g. in
front of the methods. The g refers to the Graphics object that is drawn when the
applet initializes. In Java, the period (.) after the g separates an object and its
method, or an object and its attribute.
Finally, the setBackground() method in line 25 takes a Color object and its
attribute, Color.cyan, to change the background color of the applet window. Com-
mon color words used by the setBackground() method display are show in Table 2-9.
Table 2-9
Colors Used with the Color Object
black
magenta
blue
orange
cyan
pink
darkGray
red
gray
white
green
yellow
lightGray
The setBackground() method does not start with the reference variable, g.
The object in this case is understood to be the background of the applet window
and does not have to be painted or drawn in a certain place, like the graphic.
Other related methods, such as setForeground(), setFont(), and setSize(), also
can be used to set characteristics of the applet window.
Perform the following step to enter code to declare, retrieve, and draw an
image in the applet, as well as to set the background color to cyan.
To Enter Code to Draw an Image and Set the Background Color
1. With the insertion
point in line 22,
type lines 22
through 25, as
shown in
Figure 2-54.
The edited code
is displayed
(Figure 2-55).
new code
FIGURE 2-55
 
Search WWH ::




Custom Search