Professional Documents
Culture Documents
Chapter 24 - Introduction To Java Applications and Applets: 2000 Prentice Hall, Inc. All Rights Reserved
Chapter 24 - Introduction To Java Applications and Applets: 2000 Prentice Hall, Inc. All Rights Reserved
Outline
24.1
Introduction
24.2
Basics of a Typical Java Environment
24.3
General Notes about Java and This Book
24.4
A Simple Program: Printing a Line of Text
24.5
Another Java Application: Adding Integers
24.6
Sample Applets from the Java 2 Software
Development Kit
24.7
A Simple Java Applet: Drawing a String
24.8
Two More Simple Applets: Drawing Strings and Lines
24.9
Another Java Applet: Adding Integers
24.1 Introduction
Java
Powerful, object-oriented language
Fun to use for beginners, appropriate for experience
programmers
Language of choice for Internet and network
communications
Compile
Execute
Computer (controlled by CPU) interprets program one
bytecode at a time
Performs actions specified in program
Phase 1
Editor
Disk
Program is created in
the editor and stored
on disk.
Phase 2
Compiler
Disk
Compiler creates
bytecodes and stores
them on disk.
Primary
Memory
Phase 3
Class Loader
Disk
Phase 4
Phase 5
Bytecode Verifier
Interpreter
..
..
..
Primary
Memory
..
..
..
Bytecode verifier
confirms that all
bytecodes are valid
and do not violate
Javas security
restrictions.
Interpreter reads
bytecodes and
translates them into a
language that the
computer can
understand, possibly
storing data values as
the program executes.
Performance
Comments
Prints string
String - called character string, message string, string literal
Characters between quotes a generic string
Method System.out.println
Prints a line of text in command window
When finished, positions cursor on next line
Method System.out.print
As above, except cursor stays on line
\n - newline
java Welcome1
Interprets bytecodes in Welcome1.class (.class left
out in java command)
Automatically calls main
Output types
Command window
Dialog box / Windows
Class JOptionPane
Defined in package called javax.swing
static methods
Called using dot operator (.) then method name
JOptionPane.showMessageDialog(arguments);
1
2
3
4
5 public class Welcome2 {
6
public static void main( String args[] )
7
{
8
JOptionPane.showMessageDialog(
9
null, "Welcome\nto\nJava\nProgramming!" );
10
11
System.exit( 0 ); // terminate the program
12
}
13 }
Outline
1. import statement
2. Define class
3. main
4.
JOptionPane.showMe
ssageDialog
5. System.exit
Program Output
The + operator
String concatenation - "adding" strings
"Hello" + " there " same as "Hello there"
Print variables
"myInt has a value of: " + myInt
Another version
First argument: null
Second: message to display
Third: string to display in title bar
Fourth: type of message to display
JOptionPane.PLAIN_MESSAGE
Other types in Fig. 24.7
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
Outline
import javax.swing.JOptionPane;
public class Addition {
public static void main(
{
String firstNumber,
secondNumber;
int number1,
number2,
sum;
1. import
String args[] )
//
//
//
//
//
2. main
2.1 Declare variables
2.2 showInputDialog
2.3 Assign input to
firstNumber
2.4 Repeat for
secondNumber
2.5 Convert Strings
to ints
2.6 Sum the numbers
31
JOptionPane.showMessageDialog(
32
33
JOptionPane.PLAIN_MESSAGE );
34
35
System.exit( 0 );
36
37 }
Outline
2.7 Use
showMessageDialog
to display results
Program Output
Sample Applets
Example applets
Tic-Tac-Toe
Drawing programs
Animations
See Fig. 24.8
import java.awt.Graphics
Allows program to draw graphics (lines, ovals, text) on an
applet
Benefits
Methods
paint, init, and start called automatically for all
applets
appletviewer WelcomeApplet.html
2000 Prentice Hall, Inc. All rights reserved.
import javax.swing.JApplet;
import java.awt.Graphics;
5
6
10
Outline
1. import
2. Define class
(extends JApplet)
2.1 Override paint
2.2 g.drawString
11 }
<html>
</applet>
--------------HTML file
4 </html>
</html>
Output
1
2
3
4
5
6
7
import java.awt.Graphics;
8
9
10
11
12
13 }
Outline
1. import
2. Define class
(extends JApplet)
2.1 Override paint
Program Output
Instance variables
Variables declared in body of a class (not in a method)
Each object of class gets its own copy
Can be used inside any method of the class
Method init
Called automatically in all applets
Commonly used to initialize variables
public void init()
References
Identifiers (such as myString) refer to objects
Contain locations in memory
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
//
//
//
//
Outline
1. import (note *)
2. Define class
(extends JApplet)
2.1 init
2.2 Declare variables
2.3 Input
2.4 Convert String to
double
31
32
33
34
35
36
37
38
39
40 }
<html>
3 </applet>
4 </html>
</html>
Outline
2.5 sum numbers
2.6 paint
2.7 drawRect
2.8 drawString
-------------HTML file
Outline
Program Output