Java Applet
Agenda
• Introduction
• Java Applet vs. JavaScript
• Steps in Creating a Java Applet
• Java Applet Example
• Do I have to write my own
• Summary
Introduction
• What is JAVA?
Java is an object oriented programming language created
by Sun Microsystems
• What is Java Applet?
A fully functioning small Java application
Used for Web Design
Need to be embedded into HTML file
Introduction
• What Java Applet Can do?
Display message and image on a web page
Draw picture on a web page
receive input from the user through keyboard or mouse
Play Sound
Steps in Creating a Java Applet
• Write the Java Applet Source Code
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorldApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello world!", 50, 25);
}
}
• compile it
Steps in Creating a Java Applet
• Write a HTML to call the Applet
<html>
…….
<applet CODE=“HelloWorldApplet.class" WIDTH="250"
HEIGHT="22">
</applet>
……
</html>
• Open the HTML file in browser and run
it
Java Applet Example
Java Applet Example
Introduction
Java applets are one of three kinds of
Java programs:
– An application is a standalone program
that can be invoked from the command
line.
– An applet is a program that runs in the
context of a browser session.
– A servlet is a program that is invoked on a
server program, and it runs in the context
of a web server process.
08/01/25 11
Applet Execution - 1
• An applet program is a written as a
subclass of the java.Applet class or the
javax.swing.Japplet class.
• There is no main() method in an Applet.
• An applet uses AWT for graphics, or
JApplet, a subclass of javax.swing.
08/01/25 12
Applet Execution - 2
• Life Cycle of an Applet:
– init: This method is intended for whatever
initialization is needed for an applet.
– start: This method is automatically called after init
method. It is also called whenever user returns to
the page containing the applet after visiting other
pages.
– stop: This method is automatically called whenever
the user moves away from the page containing
applets. This method can be used to stop an
animation.
– destroy: This method is only called when the
browser shuts down normally.
– Ref: http://java.sun.com/docs/books/tutorial/deployment/applet/index.html/
08/01/25 13
Applet Execution - 3
• The applet is running and rendered
on the web page.
• Every Applet needs to implement one
of more of the init(), the start( ) and
the paint( ) methods.
• At the end of the execution, the
stop( ) method is invoked, followed
by the destroy( ) method to deallocate
the applet’s resources.
08/01/25 14
Applet Security
For security reasons, applets that are loaded
over the network have several restrictions.
– an applet cannot ordinarily read or write
files on the computer that it's executing on.
– an applet cannot make network
connections except to the host that it came
from.
– Ref:
http://java.sun.com/docs/books/tutorial/deployment/applet/index.html /
08/01/25 15
HTML tags for applets - 1
<APPLET
// the beginning of the HTML applet code
CODE="demoxx.class"
// the actual name of the applet (usually a 'class' file)
CODEBASE="demos/"
// the location of the applet (relative as here, or a full URL)
NAME=“SWE622"
// the name of the instance of the applet on this page
WIDTH="100"
// the physical width of the applet on the page
HEIGHT="50"
// the physical height of the applet on the page
ALIGN="Top"
// align the applet within its page space (top, bottom, center)
08/01/25 16
HTML tags for applets - 2
<APPLET CODE=“SWE622.class" CODEBASE="example/"
WIDTH=460 HEIGHT=160
NAME="buddy" >
<PARAM NAME="imageSource" VALUE="images/Beans">
<PARAM NAME="backgroundColor" VALUE="0xc0c0c0"> <PARAM
NAME="endImage" VALUE=10>
</APPLET>
08/01/25 17
The HelloWorld Applet
<HTML> public void paint(Graphics g) {
<BODY> final int FONT_SIZE = 42;
<APPLET code=hello.class width=900 Font font = new Font("Serif",
height=300> Font.BOLD, FONT_SIZE);
</APPLET> // set font, and color and display
</BODY> message
</HTML> // on the screen at position 250,150
g.setFont(font);
// applet to display a message in a
g.setColor(Color.blue);
window
// The message in the next line is the
import java.awt.*; one
import java.applet.*; // you will see
g.drawString("Hello,
public class hello extends Applet { world!",250,150);
public void init( ) { } // end of paint()
setBackground(Color.yellow);
} // end of init() } // end of hello
08/01/25 18
Summary
• Java Applet is part of Java Programming language
• It is a useful tool for the Web Development
• There are thousands of Java Applet available online
• If you decide to use those free applets, make sure
you give the author credits