Chapter 10
Applet Programming
What is an Applet?
❖ An applet is a Java program that can be embedded into a web page.
❖ It runs inside the web browser and works at client side.
❖ An applet is embedded in an HTML page using the APPLET or OBJECT tag and hosted on a web
server.
❖ Applets are used to make the website more dynamic and entertaining.
❖ Applet = Java byte code + HTML
Running an Applet in Java
Using a web browser
a) Create an HTML file, containing the APPLET tag.
b) Compile the applet source code using javac.
c) Open the html file in the web browser.
Applets Applications
Applets don’t have main() method Applications have main() method
Applets runs under control of a java compatible An application is independently executed
container such as web browser
Applets only are executed within a Java browser or The application programs can be compatible executed using java
appletviewer interpreter
Applets can be embedded into HTML pages Applications cannot be embedded into HTML pages
Applets cannot communicate with other servers in Applications can communicate with other servers
the network
Lifecycle of an applet
Lifecycle of an applet
Lifecycle of an applet
To create an applet, we have applet class of Java.applet package
Applets have four lifecycle methods:
init(): It’s the method used to initialize the applet before it gets loaded.
start():It’s used to provide startup behavior for an applet.
stop():Its used to stop any operations which are started using start() method.
destroy(): It is called when an applet has finished its execution.
These methods are called automatically by the JVM .In the Applets life cycle methods, start and
stop methods can be called multiple times whereas init() and destroy() can be called only once in the
entire life span of an Applet.
Method 1: init() Syntax
This is the first method that is being called, All the variables are initialized here
This method is only called once during the run time
It is generally invoked during the time of initialization
Method 2: start() Syntax
It is called after the init method
Used for starting an applet
It is also used for restarting an applet in case it is stopped
Method 4: stop() Syntax
This method is invoked whenever the browser is stopped, minimized, or because of abrupt failure
within an application.
Generally after the stop method, the start method can be used.
Method 5: destroy() Syntax
Once we are done with the applet work, this method destroys the application and is invoked only
once.
Once the applet is destroyed, it can’t be restored.
How to run Applet Program
Step 1 : Write a Applet Program
Step 2 : Compile Applet Program using JAVAC Command
Step 3 : Create HTML Code and implement .class file
Step 4 : Use Applet viewer or Web Browser to view Applets
Step 1:
import java.applet.Applet;
import java.awt.Graphics;
public class MyFirstApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString(“Hello Applet”,50,60);
}
}
Step 2 : Compile applet code and generate byte code
Save the above file as MyFirstApplet.java and compile using ‘javac’. After successful compilation
‘MyFirstApplet.class’ byte code file is created.
Step 3 : Create HTML Code and implement .class file
Step 3 – Create an HTML page with the applet tag as follows
<html>
<body>
<applet code=”MyFirstApplet.class” width=440 height=200> </applet>
</body>
</html>
Save the file as AppletExample.html
Step 4 : Use Applet viewer or Web Browser to view Applets
Step 4 : Executing an applet using applet viewer & browser
To run in applet viewer, In the command prompt, type
appletviewer AppletExample.html
To run in browser, open the html page AppletExample.html from the applet enabled browser
End of Chapter