Java Applets
Introduction to Java Applet
Programs
Applications are stand alone programs
executed with Java interpreter
Applet is a small program
can be placed on a web page
will be executed by the web browser
give web pages dynamic content
Java Applets
Built using one of general definitions of
applets
Applet class
JAapplet class
Java applets are usually graphical
Draw graphics in a defined screen area
Enable user interaction with GUI elements
Java Applet Classes
Abstract
Windowing Toolkit
AWT
Earlier versions of Java
Applet class is one of the AWT components
Java Foundation
Classes JFC
Extension to Java in 1997
Has a collection of Swing components for
enhanced GUIs
Swing component classes begin with J
Java Applets
Applets are Java programs that can be
embedded in HTML documents
When browser loads Web page containing applet
To run an applet you must create a .html file which
references the applet
Ready to Program also will run an applet
Applet downloads into Web browser
begins execution
Can be tested using appletviewer program
5
Contrast Application with Applet
Application
Object
class extended
Class not declared public
Has
a main()
static keyword used
Uses
System.exit(1)
Applet
JApplet class extended
class
declared to be public
init() instead of main()
init() not declared with static
keyword
Applet Declaration
Syntax (note difference from
application
declaration)
public class ClassName extends JAapplet
ClassName is an
object that will be a
subclass of JApplet
7
Body of an Applet
Note there is no main()
method in an
applet
JApplet class provides other methods instead
of a main method
First method executed is the init()
method
Applets
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
Applications run in command windows
Notes
Focus on fundamental programming concepts first
Explanations will come later
Applets and Web Pages
HTML
Applets embedded in a web page
Executed when web page loaded by browser
Web pages structured with
HTML codes
HyperText Mark-up Language
Syntax
Turns format on
<command>
. . .
</command>
Turns format on
Turns the
the format
format off
off
Turns
10
Applets and Web Pages
HTML
Embedding Java applets
Insert applet tags
<APPLET>
</APPLET>
Call the specific applet by its file name
<APPLET CODE = "Whatever.class"
WIDTH = nnn HEIGHT = mmmm>
<\APPLET>
Where nnn and mmm are specific pixel sizes
11
Applets and Web Pages
HTML
Create the web
page code using a
text editor
Save it with an
.html suffix
Open this file with
appletviewer or with
a web browser that
supports Java
Java Plug-in must
be installed (part of
J2SDK 1.4.1 from
Sun)
<HTML>
<HEAD>
</HEAD>
<BODY>
<APPLET CODE = . . . >
</APPLET>
</BODY>
</HTML>
12
Applets and Web Pages
HTML
Client Web browser anywhere can access
this web page from its host server
Embedded Java applet runs on client
browser (of any type platform)
This means a client anywhere on any type
of platform can run a piece of software
developed on any other type of platform
Platform Independence
13
Thinking About Objects
Java an object-oriented language
However, Java has constructs from structured
programming
Object orientation
Natural way to think about world and writing computer
programs
Object-oriented programming models the real world
Attributes - properties of objects
Size, shape, color, weight, etc.
Behaviors - actions that objects can perform
Aball rolls, bounces, inflates and deflates
14
Thinking About Objects
Object orientation (continued)
Inheritance
New classes of objects absorb characteristics of
existing classes
Information hiding
Objects usually do not know how other objects are
implemented
We can drive cars without knowing how every part
works internally
15
Thinking About Objects
Class - unit of Java programming
Java focuses on nouns (classes)
Contain methods
Implement behaviors
Contain data
C focuses on verbs and is action oriented
Implement attributes
Classes are reusable
Standardized, interchangeable parts
16
A Simple Java Applet: Drawing a String
Figure 3.6
a welcome message applet
The .html code to run the applet in a
browser
<html>
<html>
<applet
<applet code
code == "WelcomeApplet.class"
"WelcomeApplet.class" width
width == "300"
"300" height
height == "45">
"45">
</applet>
</applet>
</html>
</html>
The program
output shown in the Applet
Viewer
17
Running the Applet
Compile
We must create an HTML file
Use Ready to Program
If no errors, bytecodes stored in
WelcomeApplet.class
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
18
Running the Applet - Alternatively
Run from
within Ready to Program
Prompt for applet window size appears
Applet window runs
19
import allows us to use
predefined classes (allowing
us to use applets and
graphics, in this case).
extends allows us to inherit the
capabilities of class JApplet.
Method paint is guaranteed to
be called in all applets. Its first
line must be defined as above.
20
Running An Applet
import
import java.applet.Applet;
java.applet.Applet;
import
import java.awt.Graphics;
java.awt.Graphics;
public
public class
class HelloApplet
HelloApplet extends
extends Applet
Applet {{
public
public void
void paint
paint (Graphics
(Graphics g)
g)
{{
g.drawString
g.drawString ("Hello.
("Hello. Welcome
Welcome to",25,25);
to",25,25);
g.drawString
g.drawString ("Java
("Java Programming",25,40);
Programming",25,40);
}}
}}
Enter
Enterthis
thistext
textinto
intoyour
your
Ready
Readyto
toProgram
Program editor
editor
Compile
Compilethe
theJava
Javacode
code
21
Running An Applet
Now create an .html file to run the applet
<html>
<html>
<applet
<applet code
code == "HelloApplet.class"
"HelloApplet.class" width=275,
width=275, height
height == 100>
100>
</applet>
</applet>
</html>
</html>
Save it as
HelloApplet.html
Make sure you save it in the same
directory as the .java file
22