Chapter 2 Java
Applet
2 Course outline
Chapter 2 Java Applet
2.1. Overview of Java Applets
2.2. Java Applets Vs Java Application
Overview of
3 Applet
Java programs that run on the Web are called applets;
An applet is a small java program that are not stand-alone programs. Instead, they run
within either a web browser or an applet viewer.
Java can work within the World Wide Web of computers via browsers, such as Netscape and
Internet Explorer, google chrome etc. Applets are usually run from web browsers.
The JVM is able to run a Java program for the particular computer on which it is running.
An applet is embedded in an HTML page using the APPLET or OBJECT tag and hosted on
a
web server.
Applets provide sound, graphics and animation in various forms and formats for web pages.
Overview of Applet cont…
4
Applets are used to make the website more dynamic and entertaining, also used in
games, gaming consoles, commercial websites, learning tools and many more.
All applets are sub-classes of the java.applet.Applet class
Swing provides a newer subclass of the Applet class called javax.swing.Japplet so
more functionality is inherited by extending the JApplet class
JDK provides a standard applet viewer tool called applet viewer.
In general, execution of an applet does not begin at main() method.
Output of an applet window is not performed by System.out.println(). Rather it is
handled with various AWT methods, such as drawString().
Life cycle of an applet :
5
It is important to understand the order in which the
various methods shown here in the image are called.
When an applet begins, the following methods are called, in
this sequence:
1. init( )
2. start( )
3. paint( )
When an applet is terminated, the following sequence of
method calls takes place:
1.stop( )
2.destroy( )
Life cycle of an applet cont…
6
init(): This method is intended for whatever initialization is needed for your applet. It is
called after the param tags inside the applet tag have been processed.
start(): This method is automatically called after the browser calls the init() method. It is
also called whenever the user returns to the page containing the applet after having
gone off to other pages.
stop(): This method is automatically called when the user moves off the page on which
the applet sits. It can, therefore, be called repeatedly in the same applet.
destroy(): This method is only called when the browser shuts down normally. Because
applets are meant to live on an HTML page, you should not normally leave resources
behind after a user leaves the page that contains the applet.
7
• paint(): is invoked immediately after the start() method, and also
any time the applet needs to repaint itself in the browser. The paint()
There many types of web applications and numerous ways in which applets may
method is actually inherited from the java.awt
be applied.
They are used in games, gaming consoles, commercial websites etc. So you can surf
further if you are interested in it.
Java Applets Vs Java
8 Application
Java Applets Example
9 import java.applet.*;
import java.awt.*;
public class HelloWorldApplet extends Applet {
public void paint (Graphics g) {
g.drawString ("Hello World This is First Java
Applet Program", 10, 50);
To run the applet program
}
javac HelloWorldApplet.java
} appletviewer RunAppletHelloWorld.html
Save this html as “RunAppletHelloWorld.html”
import java.awt.*;
import java.applet.*;
public class Home extends Applet{
public void paint(Graphics g){
int x[]={150,300,225};
int y[]={150,150,25};
g.drawRect(150,150,150,200);
g.drawRect(200,200,50,150);
g.drawOval(200,75,50,50);
g.drawPolygon(x,y,3);
}
}
<html>
<head>
</head>
<body>
<Applet
code=Home.class width=1200 height=1200
>
</Applet>
</body>
</html>
10
Processed to Chapter Three