Init Method: The Life Cycle of An Applet Begins When The Applet Is First Loaded Into The
Init Method: The Life Cycle of An Applet Begins When The Applet Is First Loaded Into The
Applet is java program that can be embedded into HTML pages. Java applets runs
on the java enables web browsers such as mozila and internet explorer. Applet is
designed to run remotely on the client browser, so there are some restrictions on it.
Applet can't access system resources on the local computer. Applets are used to
make the web site more dynamic and entertaining.
Advantages of Applet:
Applets are cross platform and can run on Windows, Mac OS and Linux
platform
Applets can work all the version of Java Plugin
Applets runs in a sandbox, so the user does not need to trust the code, so it can
work without security approval
Applets are supported by most web browsers
Applets are cached in most web browsers, so will be quick to load when
returning to a web page
User can also have full access to the machine if user allows
Life Cycle of Applet: Applet runs in the browser and its lifecycle method are called
by JVM when it is loaded and destroyed. Here are the lifecycle methods of an
Applet:
Start () method: The start method of an applet is called after the initialization method init().
This method may be called multiple times when the Applet needs to be started or restarted.
For Example if the user wants to return to the Applet, in this situation the start Method() of an
Applet will be called by the web browser and the user will be back on the applet. In the start
method user can interact within the applet.
Stop () method: The stop() method can be called multiple times in the life cycle of applet like
the start () method. Or should be called at least one time. There is only miner difference
between the start() method and stop () method. For example the stop() method is called by the
web browser on that time When the user leaves one applet to go another applet and the start()
method is called on that time when the user wants to go back into the first program or Applet.
destroy() method: The destroy() method is called only one time in the life cycle of
Applet like init() method. This method is called only on that time when the browser
needs to Shut down.
Creating First Applet Example: First of all we will know about the applet. An
applet is a program written in java programming language and embedded within
HTML page. It runs on the java enabled web browser such as Netscape navigator or
Internet Explorer.
In this example you will see, how to write an applet program. Java source of applet
is then compiled into java class file and we specify the name of class in the applet
tag of html page. The java enabled browser loads class file of applet and run in its
sandbox.
Here is the java code of program :
import java.applet.*;
import java.awt.*;
public class FirstApplet extends Applet{
public void paint(Graphics g){
g.drawString("Welcome in Java Applet.",40,20);
}
}
<HTML>
<HEAD>
<TITLE>Passing Parameter in Java Applet</TITLE>
</HEAD>
<BODY>
This is the applet:<P>
<APPLET code="appletParameter.class" width="800"heigh
t="100">
<PARAM name="message" value="Welcome in Passing
parameter in java applet example.">
</APPLET>
</BODY>
</HTML>
There is the advantage that if need to change the output then you will have to
change only the value of the param tag in html file not in java code.