Title: Chapter 3: Introduction to Applets
1Xavier University Computer Center Sun
Microsystems, Philippines
Java sa Eskwela (JsE) Program
2Chapter 3
Introduction to Applets
3Chapter 3
- Outline
- A Simple Java Applet Drawing a String
- Two More Simple Applets Drawing Strings and
Lines - Another Java Applet Adding Integers
4Objectives
- To understand the difference between an applet
and an application - To be able to write simple Java applets
- To be able to write simple HyperText Markup
Language (HTML) files to load an applet into the
appletviewer or a web browser
5Applets
- Program that runs in
- appletviewer (test utility for applets)
- Web browser (IE, Communicator)
- Executes when HTML (Hypertext Markup Language)
document containing applet is opened - Applications run in command windows
6Applets
7Applets
- General Process
- an instance of the applet's controlling class (an
Applet subclass) is created - the applet initializes itself
- the applet starts running
- the applet stops running
- the applet does final clean-up then unloads
8Applets
- General Structure
-
- public class Simple extends JApplet
- public void init() ...
- public void start() ...
- public void stop() ...
- public void destroy() ...
-
9Applets
- init()
- to initialize the applet each time it's loaded
(or reloaded) - start()
- to start the applet's execution, such as when the
applet's loaded or when the user revisits a page
that contains the applet
10Applets
- stop()
- to stop the applet's execution, such as when the
user leaves the applet's page or quits the
browser - destroy()
- to perform a final cleanup in preparation for
unloading
11Applets
- paint()
- basic display method
- many applets implement the paint method to draw
the applet's representation within a browser page - update()
- used along with paint to improve drawing
performance
12Applets
- Program Analysis
- Create an applet to display
- "Welcome to Java Programming!"
- Show applet and HTML file, then discuss them line
by line
13// Fig. 3.6 WelcomeApplet.java // A first applet
in Java import javax.swing.JApplet // import
class JApplet import java.awt.Graphics //
import class Graphics public class WelcomeApplet
extends JApplet public void paint(
Graphics g ) g.drawString( "Welcome to
Java Programming!", 25, 25 )
14//Open WelcomeApplet.class in a browser through
an //HTML file lthtmlgt ltapplet
code"WelcomeApplet.class" width300
height30gt lt/appletgt lt/htmlgt
15Applets
- Methods paint, init, and start
- guaranteed to be called automatically for us
- our applet gets a "free" version of these by
inheriting from JApplet - free versions have an empty body (do nothing)
- every applet does not need all three - override
only the ones you need
16Applets
- Method paint
- draws graphics on screen
- void means paint returns nothing when it finishes
its 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
17Applets
- Body of paint
- method drawString (of class Graphics)
- called using Graphics object g and dot operator
(.) - method name followed by parenthesis containing
argument list - First argument String to draw
- Second x coordinate of location to draw at (in
pixels) - Third y coordinate of location to draw at (in
pixels)
18Applets
- Java coordinate system
- measured in pixels (picture elements)
- upper left is (0,0)
19Applets
- Running the applet
- Compile
- javac WelcomeApplet.java
- if no errors, bytecodes stored in
WelcomeApplet.class - we must create an HTML file
- loads the applet into appletviewer or a browser
- ends in .htm or .html
- to execute an applet
- create an HTML file indicating which applet the
browser (or appletviewer) should load and execute
20Applets
- Program Analysis
- mimics application for adding two integers
- this time, use floating point numbers (numbers
with a decimal point) - show program, then we will discuss it
21// Fig. 3.12 AdditionApplet.java // Adding two
floating-point numbers import java.awt.Graphics
// import class Graphics import javax.swing.
// import package javax.swing public class
AdditionApplet extends JApplet double sum
// sum of the values entered by the user
public void init() String firstNumber,
// first string entered by user
secondNumber // second string entered by user
double number1, // first number to add
number2 // second number to
add // read in first number from user
firstNumber JOptionPane.showInputDialo
g( "Enter first floating-point value"
)
22 // read in second number from user
secondNumber JOptionPane.showInputDialo
g( "Enter second floating-point
value" ) // convert numbers from type
String to type double number1
Double.parseDouble( firstNumber ) number2
Double.parseDouble( secondNumber ) //
add the numbers sum number1 number2
public void paint( Graphics g )
// draw the results with g.drawString
g.drawRect( 15, 10, 270, 20 )
g.drawString( "The sum is " sum, 25, 25 )
23(No Transcript)
24Applets
- Method init
- Normally initializes instance variables
- Guaranteed to be first method called
- First line must always appear as above
- Returns nothing (void), takes no arguments
25Applets
- static method Double.parseDouble
- converts String argument to a double
- returns the double value
- Method drawRect( x1, y1, width, height )
- draws a rectangle with an upper left corner (x1,
y1), with specified width and height