2. 2
Topics
What is Applet?
Difference b/w Application and Applet
Restrictions
Life Cycle of Applet
Applet States and Methods
Applet program Structure
First Applet and Execution
Embedding Applet in Web Page
Displaying images using Applets
Passing Parameters to Applet
More Samples!!!
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
3. 3
Applets
An applet (little application) is a small program which
should be run within a larger program.
An applet is a program that is typically embedded in a
Web page and can be run from a browser
They can be transported over the Internet from one
computer (web server) to another (client computers).
You need special HTML tag in the Web page to tell the
browser about the applet
Created by subclassing from the
java.applet.Applet class
Examples of Java enabled web browsers are Internet
Explorer, Firefox, chrome (only older versions)
Pig is to piglet as Application is to ??
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
4. 4
Applet: Accessed over the Web
Hello
Hello Java
<app=
“Hello”>
4
APPLET
Development
“hello.java”
AT
SUN.COM
The Internet
hello.class
AT SUN’S
WEB
SERVER
2 31 5
Create
Applet
tag in
HTML
document
Accessing
from
Your Organisation
The browser
creates
a new
window and
a new thread
and
then runs the
code
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
5. Difference - Applets and Applications
An applet is basically designed for deploying on the web.
An application is designed to work as a standalone program.
Applets are created by extending the java.applet.Applet class
There is no such constraint for an application.
Applets run on any browser.
Applications run using Java interpreter/terminal.
Execution of applets begin with the init() method. Execution
of applications begins with main() method.
It is not mandatory to declare main() for an applet.
In case of application, main() has to be included in a public
class.
Output to an Applet’s window is done by using different AWT
methods such as drawString().
In case of an application System.out.println() method is used.
5Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
6. Difference - Applets and Applications
6Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
7. 7
Restrictions
Although both the Applets and stand-alone applications
are Java programs, there are certain restrictions are
imposed on Applets due to security concerns:
They are embedded inside a web page and executed in
browsers.
They cannot read from or write to the files on local
computer.
They cannot communicate with other servers on the
network.
They cannot run any programs from the local
computer.
They are restricted from using libraries from other
languages.
The above restrictions ensures that an Applet cannot do
any damage to the local system.Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
8. Life cycle of an Applet
Life cycle of an Applet specifies stages the object has
to pass right from its creation until it is destroyed.
Every applet inherits a set of default behaviours from
the Applet class. As a result, when an applet is
loaded, it undergoes a series of changes in its state.
For each event/state, a method is automatically
called.
The applet states include:
Initialisation – invokes init()
Running – invokes start()
Display – invokes paint()
Idle – invokes stop()
Dead/Destroyed State – invokes destroy()
8Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
9. 9
Applet States
Initialisation – invokes init() – only once
Invoked when applet is first loaded.
Running – invokes start() – more than once
For the first time, it is called automatically by the
system after init() method execution.
It is also invoked when applet moves from idle/stop()
state to active state.
Display – invokes paint() - more than once
It happens immediately after the applet enters into the
running state. It is responsible for displaying output.
Idle – invokes stop() - more than once
It is invoked when the applet is stopped from running.
For example, it occurs when we leave a web page.
Dead/Destroyed State – invokes destroy() - only once
This occurs automatically by invoking destroy()
method when we quite the browser.
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
10. Redraw
Applet
stop( )
Start
state
start( ) paint( )
Life cycle of an Applet Contd…
Applet
Working
Applet
Born
Applet
Displayed
Idle
State
Applet
Destroyed
Initialization
state
destroy( )
Destroy
Appletinit( )
10Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
11. 11
Applet Life Cycle Diagram
Born
Running Idle
Dead
Begin
init()
start()
paint()
stop()
start()
destroy()
End
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
13. 13
Building Applet Code: An Example
//HelloWorldApplet.java
import java.applet.Applet;
import java.awt.*;
public class HelloApplet extends Applet {
public void paint(Graphics g) {
g.drawString ("Hello World of Java!",25, 25);
}
}
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
14. 14
Embedding Applet in Web Page
<HTML>
<HEAD>
<TITLE>
Hello World Applet
</TITLE>
</HEAD>
<body>
<h1>Hi, This is My First Java Applet on the Web!</h1>
<applet code="HelloApplet.class" width=500 height=400>
</applet>
</body>
</HTML>
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
15. 15
Accessing Web page (runs Applet)
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
16. A simple applet
Output
import java.awt.*;
import java.applet.*;
public class FirstApplet extends Applet
{
String str;
public void init()
{
str = "Java is interesting!";
}
public void paint(Graphics g)
{
g.drawString(str, 70, 80);
}
}
16Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
17. Create a HTML page to display the applet
<html>
<applet code=FirstApplet.class width=200 height=200>
</applet>
</html>
Then type the following at command prompt:
appletviewer abc.html where abc.html is the name of
the html file.
Creating an Applet
An applet is compiled using the Java compiler: javac
javac FirstApplet.java
17Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
18. Displaying images using Applets
Output
/*
<applet code = DisplayImage width = 200 height = 200>
</applet>
*/
import java.awt.*;
import java.applet.*;
public class DisplayImage extends Applet
{
Image img;
public void init()
{
img = getImage(getCodeBase(),"duke.gif");
}
public void paint(Graphics g)
{
g.drawImage(img,20,20,this);
}
}
18Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
19. Displaying images using Applets
To display images, we need to make use of the Image
and Graphics classes.
getCodeBase() method gets the base URL of the applet
getImage() method returns an Image object which can
be drawn on the screen
drawImage() takes four parameters – Image object,
location in terms of x and y coordinates and an object
of type ImageObserver
19Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
20. 20
Sample Graphics methods
A Graphics is something you can paint on
g.drawRect(x, y, width, height);
g.fillRect(x, y, width, height);
g.drawOval(x, y, width, height);
g.fillOval(x, y, width, height);
g.setColor(Color.red);
g.drawString(“Hello”, 20, 20); Hello
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
21. 21
repaint( )
Call repaint( ) when you have changed something
and want your changes to show up on the screen
You do not need to call repaint() when something in
Java’s own components (Buttons, TextFields, etc.)
You do need to call repaint() after drawing commands
(drawRect(...), fillRect(...), drawString(...), etc.)
repaint( ) is a request--it might not happen
When you call repaint( ), Java schedules a call to
update(Graphics g)
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
22. 22
Passing Parameters to Applet
<HTML>
<HEAD>
<TITLE>
Hello World Applet
</TITLE>
</HEAD>
<body>
<h1>Hi, This is My First Communicating Applet on the Web!</h1>
<APPLET
CODE="HelloAppletMsg.class" width=500 height=400>
<PARAM NAME="Greetings" VALUE="Hello Friend, How are you?">
</APPLET>
</body>
</HTML>Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
23. 23
Applet Accepting Parameters
//HelloAppletMsg.java
import java.applet.Applet;
import java.awt.*;
public class HelloAppletMsg extends Applet {
String msg;
public void init()
{
msg = getParameter("Greetings");
if( msg == null)
msg = "Hello";
}
public void paint(Graphics g) {
g.drawString (msg,10, 100);
}
} This is name of parameter specified in PARAM tag;
This method returns the value of paramter.
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam
25. 25
Displaying Numeric Values
//SumNums.java
import java.applet.Applet;
import java.awt.*;
public class SumNums extends Applet {
public void paint(Graphics g) {
int num1 = 10;
int num2 = 20;
int sum = num1 + num2;
String str = "Sum: "+String.valueOf(sum);
g.drawString (str,100, 125);
}
}
Dr. P. Victer Paul, Indian Institute of Information Technology Kottayam