jhtp4 03
jhtp4 03
3.1 Introduction
• Applet
– Program that runs in
• appletviewer (test utility for applets)
• Web browser (IE, Communicator)
– Executes when HTML (Hypertext Markup Language)
document containing applet is opened and downloaded
– Applications run in command windows
• Notes
– Mimic several features of Chapter 2 to reinforce them
– Focus on fundamental programming concepts first
• Explanations will come later
• Running applets
– In command prompt, change to demo subdirectory of applet
cd c:\jdk1.3\demo\applets
cd appletDirectoryName
– There will be an HTML file used to execute applet
– Type appletviewer example1.html
• appletviewer loads the html file specified as its command-
line argument
• From the HTML file, determines which applet to load (more
section 3.3)
– Applet will run, Reload and Quit commands under
Applet menu
Program Output
– Comments
• Name of source code and description of applet
5 import java.awt.Graphics; // import class Graphics
8 import javax.swing.JApplet; // import class JApplet
– Method paint
• Lines 13-21 are the definition of paint
• Draws graphics on screen
• void indicates paint returns nothing when finishes task
• Parenthesis define parameter list - where methods receive data
to perform tasks
– Normally, data passed by programmer, as in
JOptionPane.showMessageDialog
• paint gets parameters automatically
– Graphics object used by paint
• Mimic paint's first line
– Body of paint
• Method drawString (of class Graphics)
• Called using Graphics object g and dot operator (.)
• Method name, then parenthesis with arguments
– First argument: String to draw
– Second: x coordinate (in pixels) location
– Third: y coordinate (in pixels) location
– Java coordinate system
• Measured in pixels (picture elements)
• Upper left is (0,0)
Program Output
Program Output
Program Output
– Method init
• Normally initializes instance variables and applet class
• Guaranteed to be first method called in applet
• First line must always appear as above
– Returns nothing (void), takes no arguments
15 {
– Declare variables
– Two types of variables
• Reference variables (called references)
– Refer to objects (contain location in memory)
• Objects defined in a class definition
• Can contain multiple data and methods
– paint receives a reference called g to a Graphics
object
– Reference used to call methods on the Graphics object
• Primitive data types (called variables)
– Contain one piece of data
• Method JOptionPane.showInputDialog
• Prompts user for input with string
• Enter value in text field, click OK
– If not of correct type, error occurs
– In Chapter 14 learn how to deal with this
• Returns string user inputs
• Assignment statement to string
– Lines 26-27: As above, assigns input to secondNumber
– Assignment statement
• sum an instance variable, can use anywhere in class
– Not defined in init but still used
status bar
Fig. 3.17 Selecting the template used to convert the HTML files.
Elevator
Turns
on/ off
Walks
ac ross
1 1
ElevatorShaft Re sets
FloorDo or FloorButto n
2 1 1 2
1
1 1 1
1 0..*
Presses 1
Signals Req uests Perso n
O p ens a rrival Presses 1
1
1 1 1 Signa ls to 1
O pens move
Eleva to rDoor Elevator Ele va to rButton
1 1 1 Resets 1
1 1
Ring s
Rid es
1
Bell
: ElevatorSha ft
: Bell