Java Reference
In-Depth Information
1
/*
2
Chapter 2:
Welcome to My Day
3
Programmer: J. Starks
4
Date:
October 4, 2007
5
Filename:
WelcomeApplet.java
6
Purpose:
This project displays a welcome message, the user's
7
name, the system date, and an image in an applet.
8
*/
9
10
import java.util. Date ;
11
import java.awt.*;
12
import java.applet.*;
13
14
public class WelcomeApplet extends Applet
15
{
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 ) ;
22
Image smile; // declare an Image object
23
smile = getImage ( getDocumentBase () , "Smile.gif" ) ;
24
g.drawImage ( smile,10,10, this ) ;
25
setBackground ( Color .cyan ) ;
26
}
27
}
FIGURE 2-48
Perform the following steps to code the import statements to support the
applet.
To Code Import Statements
1. With the Welcome.java file displayed in the TextPad coding window,
click at the end of line 10 and then press the ENTER key.
2. Type lines 11 and 12, as shown in Figure 2-48.
The applet will import objects and classes from the applet and awt
packages, as well as from the Date package.
The remaining edits to the source code will incorporate methods and classes
that Java uses from the applet and awt packages.
Changing the Class Name and Extending the Applet Class
Because the purpose of this program now will be to run as an applet on
the Web, it is important to change the class name and the file name. The class
name is changed to WelcomeApplet in the class header. The file name, which is
changed to WelcomeApplet.java, must be updated in the comment header and
when the file is saved.
Search WWH ::




Custom Search