Applet
Applet
Applet
❖An Applet is a program that runs in a web browser.
❖They can be transported over the internet from one computer to
another computer and run using the applet the appletviewer or any
web browser that supports java.
❖Applet can be used to perform arithmetic operations,display
graphics,play sounds,accept user input,create animation and play
interactive games like any other applications.
❖An Applet is a java class that extends the java.applet.Applet class.
Summary
• An applet is a program written in the java programming language that
can be included in ana HTML page,much in the same way an image is
included in a page. When you use a Java technology-enabled browser
to view a page that contains an applet, the applet’s code is
transferred to your system and executed by the browser’s JVm.
Difference between applet and Application
❖Applet don’t use the main method for initiating the execution of the
code.
❖When the applet is loaded, it automatically calls certain methods of
applet class to start and execute the applet code.
❖Unlike stand-alone application, applets cannot be run independently.
They run from inside a web page using a special features known as
HTML tag
❖A JVM is required to view an applet.
When to use Applet
• When we need something dynamic in the display of a web page.
• When we want to create a program and make it available on the
internet for use by others on their computers.
Applet hierarchy
Life cycle of Applet
❖Applet life cycle defined as how the object created, started, stopped and
destroyed during the entire execution of the application is said to applet
life cycle.
Methods of Applet Life Cycle
Applet life cycle has 5 methods.
❖ init()
❖start()
❖ paint()
❖ stop()
❖ destroy()
Continue..
Continue..
• When ever applet is loaded,first of all init() method is called,then
start() method,after that paint() method is called,the stop method()
and at last destroy method is called.
• All this methods are under Applet class.
• These methods are invoked by the browser to execute. Applet works
on the client side so, less processing time.
Continue…
❖init():
➢init() method is used to initialize
➢It is invoking only once at the time of initialization.
Syntax:
public void init()
{
//initialized objects
}
❖start():
➢start() method is used to start the applet.
➢It is invoking after the init()method invoked.
➢ It is invoking each time when browser loading or refreshing.
Syntax:
public void start()
{
//start the applet code
}
Continue…
❖paint():
➢paint() method is used for painting any shapes like square, rectangle,
trapezium, eclipse, etc.
➢paint() method has parameter as ClassGraphics.
➢This Graphics class gives painting features in an applet.
Syntax:
public void paint(Graphics g)
g.drawString(“hello”);
{
//any shapes code
}
❖Stop():
➢stop() method is used to stop the applet.
➢It is invoked every time when browser stopped or minimized or abrupt
failure of the application.
➢After stop()method called, we can also start() method whenever we want.
Syntax:
public void stop(){
//stop the applet code
}
❖destroy():
➢ destroy() method is used to destroy the application once we have
done with our applet work.
➢ It is invoked only once.
➢Once applet is destroyed we can’t start()the applet.
Syntax:
public void destroy(){
//destroy the applet code
}
Steps to create and use an Applet
• Building an applet code(.java file)
• Creating an executable applet(.class file)
• Designing a web page using HTML tags.
• Adding <APPLET> tag and attributes inside applet tag into the web
page.
• Creating html file or you can embed within java file also
• Testing the applet code.
Continue…
Applet can be embedded into web pages in two ways as:
• We can write our own applet and embed them into web pages.
• We can download an applet from a remote computer system and embed it
into the web pages.
Types of Applet:
Local Applet
Remote Applet
Local Applet
❖Applets developed in a local computer are known as local applet.
❖To run local applet in local computer, we don’t need internet.
❖It Simply searches the directories in the local system and locates and
loads the specified applet.
Remote Applet
❖Applets which are developed by someone else and stored on a
remote computer connected to the internet are known as remote
applet.
❖To run the remote applet to the local computer, the local system
must be connected with internet.
❖We can download remote applet onto the local computer via
internet and run it.
Example accessing local and remote applet from html file as shown below:
Applet program:
import java.awt.Graphics;
import java.applet.Applet;
public class HelloApplet extends Applet{
String msg=“Welcome to Applet programming”;
public void paint(Graphics g){
g.drawstring(msg,100,100);
}
}
Html tags for remote applet:
<html>
<head><title></title></head>
<body>
<applet name=“remoteApplet” codebase=“www.mysite.com.np\applet”
code=“HelloApplet.class” width=“400” height=“400”></applet>
</body>
</html>
}
Passing Parameter to Applets
Example shows how the html file is sending parameter and how the java code handles these
parameter at runtime.