Académique Documents
Professionnel Documents
Culture Documents
Applets
Outline
3.1
Introduction
3.2
Sample Applets from the Java 2 Software Development Kit
3.3
Simple Java Applet: Drawing a String
3.4
Drawing Strings and Lines
3.5
Adding Floating-Point Numbers
3.6
Java Applet Internet and World Wide Web Resources
3.7
(Optional Case Study) Thinking About Objects: Identifying
the Classes in a Problem Statement
3.1
Introduction
Applet
Program that runs in
appletviewer (test utility for applets)
Web browser (IE, Communicator)
Notes
Mimic several features of Chapter 2 to reinforce them
Focus on fundamental programming concepts first
3.2
Sample Applets
Provided in Java 2 Software Development Kit (J2SDK)
Source code included (.java files)
Study and mimic source code to learn new features
All programmers begin by mimicking existing programs
3.2
Running applets
In command prompt, change to demo subdirectory of applet
cd c:\j2sdk1.4.1\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)
3.2
Fig. 3.2
3.2
Select the
drawing color by clicking
the circle for the color
you want. These GUI
components are
commonly known as
radio buttons.
3.2
Try changing
the options to
see their effect
on the
demonstration.
3.3
Upcoming program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Outline
Java applet
//
// import class JApplet
Program Output
3.3
1
2
Comments
Name of source code and description of applet
5
6
import java.awt.Graphics;
import javax.swing.JApplet;
10
3.3
11
3.3
8
12
3.3
8
13
3.3
11
14
3.3
11
Method paint
15
3.3
14
Body of paint
Method drawString (of class Graphics)
Called using Graphics object g and dot (.)
Method name, then parenthesis with arguments
First argument: String to draw
Second: x coordinate (in pixels) location
Third: y coordinate (in pixels) location
16
3.3
To execute an applet
17
3.3
1
2
3
4
18
3.3
1
2
3
4
appletviewer WelcomeApplet.html
Perform in directory containing .class file
19
3.3
20
3.4
More applets
First example
Display two lines of text
Use drawString to simulate a new line with two
drawString statements
Second example
21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Outline
2
2
WelcomeApplet2.j
ava
1. import
2. Class
WelcomeApplet2
(extends JApplet)
3. paint
3.1 drawString
3.2 drawString
The two drawStringon same x coordinate,
but 15 pixels
statements simulate a newline.
In down
fact, the concept of lines of text
does not exist when drawing
strings.
Outline
1
2
3
4
<html>
<applet code = "WelcomeApplet2.class" width = "300" height = "60">
</applet>
</html>
HTML file
Program Output
2
3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Outline
WelcomeLines.ja
va
2. Class
WelcomeLines
(extends
JApplet)
3. paint
2
4
3.1 drawLine
3.2 drawLine
Draw horizontal lines with
drawLine (endpoints
have same
3.3 drawString
y coordinate).
Program Output
2003 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
Outline
<html>
<applet code = "WelcomeLines.class" width = "300" height = "40">
</applet>
</html>
HTML file
2
5
3.4
26
3.5
Next applet
Mimics application for adding two integers (Fig 2.9)
This time, use floating point numbers (numbers with a decimal
point)
Using primitive types
double double precision floating-point numbers
float single precision floating-point numbers
27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Outline
2
3 import java.awt.Graphics;
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Java packages
import java.awt.Graphics;
// import class Graphics
import javax.swing.*;
import package
public
class AdditionApplet//extends
JAppletjavax.swing
{
double sum;
AdditionApplet.
java
1. import
2. Class
AdditionApplet
(extends
JApplet)
2
8
3. Fields
4. init
store floating
firstNumber = JOptionPane.showInputDialog(
first
floating-point
value"
//"Enter
read in
second
number from
user);
4.2
showInputDialog
secondNumber =
// obtain
second number from user
JOptionPane.showInputDialog(
secondNumber
= JOptionPane.showInputDialog(
"Enter
second floating-point value" );
"Enter second floating-point value" );
4.3 parseDouble
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
1
2
3
4
Outline
//
//add
addnumbers
the numbers
sum
+ number2;
sum= =number1
number1
+ number2;
31
32
33
34
35
public
void paint(
Graphics on
g )applets background
//
draw results
in a rectangle
{
public
void paint( Graphics g )
{
// draw the results with g.drawString
//
call superclass
version
paint
g.drawRect(
15, 10,
270,of20method
);
super.paint( g );
5. Draw applet
contents
36
37
38
39
40
41 }
1 <html>
2 <applet
code="AdditionApplet.class"
width=300
height=50>
// draw
results as a String at (25,
25)
3 </applet>
g.drawString( "The sum is " + sum, 25, 25 );
4 </html>
} // end method paint
} // end class AdditionApplet
<html>
<applet code = "AdditionApplet.class" width = "300" height = "65">
</applet>
</html>
2
9
Outline
Program Output
3
0
3.5
import java.awt.Graphics;
import javax.swing.*;
31
3.5
8
double sum;
Field declaration
32
3.5
9
double sum;
Method init
Normally initializes fields and applet class
Guaranteed to be first method called in applet
First line must always appear as above
Returns nothing (void), takes no arguments
13
33
3.5
14
15
16
17
18
double number1;
double number2;
Declare variables
Two types of variables
34
3.5
14
15
16
17
18
double number1;
double number2;
35
3.5
21
22
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 15 learn how to deal with this
Returns string user inputs
Assignment statement to string
36
3.5
29
30
Assignment statement
37
3.5
35
38
3.5
48
39
3.6
www.jars.com
40
elevator system
office building
elevator shaft
elevator
floor door
software-simulator application
bell inside the elevator
passenger
light on that floor
user of our application
energy
floor button
capacity
elevator button
elevator car
Fig. 3.15 Nouns (and noun phrases) in problem statement.
41
42
43
44
Resets
Signals
arrival
2
1
2
FloorButton
Requests
1
Elevator
45
Symbol
Meaning
0
None
1
One
m
An integer value
0..1
Zero or one
m, n
m or n
m..n
At least m, but not more than n
*
Zero or more
0..*
Zero or more
1..*
One or more
Fig. 3.18 Multiplicity types.
46
Aggregation relationship
47
Walks
across
Turns on/off
Light
1
Resets
ElevatorShaft
FloorDoor
FloorButton
1
Presses
Signalsarrival
Requests
Presses
ElevatorDoor
Opens
1
1
Signals to
move
Elevator
1
Resets
ElevatorButton
1
Rings
Rides
1
Bell
1
1
1
Person
Opens
passenger
48
49