Java Reference
In-Depth Information
class header displays
extends command
edited
comments
new file
name
FIGURE 2-50
The relationship of the SDK to its packages, and the packages to their
classes, is a perfect example of the superclass, class, and subclass hierarchy
discussed in Chapter 1. In a later chapter, you will learn more about creating
subclasses and instances of existing classes.
The paint() Method
As you have learned, the main() method is the first method called when any
stand-alone Java application is executed. With an applet, Java does not look for a
main() method because Java does not execute the applet; the applet is executed
from a browser or other calling program. Instead, the init() method loads the
initial setup of the applet when execution begins. Because the code extends the
Applet class, init() and all other applet methods are encapsulated in the Applet
class, which means they happen automatically without any additional code being
added to the program.
In the Welcome to My Day applet, you also will code a paint() method to
graphically draw text and an image on the applet screen after the applet is initial-
ized. The source code for the beginning of the paint() method is displayed in
lines 16 through 21 in Figure 2-51. As shown in line 16, the paint() method
accepts a Graphics parameter. It is common practice to identify the Graphics
parameter as g, although any variable name can be used. The paint() method
returns no value, so it is void.
16
public void paint ( Graphics g )
17
{
18
Date currentDate = new Date () ; // Date constructor
19
g.drawString ( "Welcome to my day!" ,200,70 ) ;
20
g.drawString ( "Daily planner for Linda Nunez" ,200,100 ) ;
21
g.drawString ( currentDate.toString () ,200,130 ) ;
FIGURE 2-51
 
Search WWH ::




Custom Search