UNIT-I Applet
UNIT-I Applet
UNIT-I Applet
UNIT –I
JAVA APPLET
Applet Application
Small Program Large Program
Used to run a program on client Can be executed on stand alone
Browser computer system
Applet is portable and can be executed Need JDK, JRE, JVM installed
by any JAVA supported browser. on client machine.
Applet applications are executed in a Application can access all the
Restricted Environment resources of the computer
Applications are created by
Applets are created by extending the
writing public static void
java.applet.Applet
main(String[] s) method.
Applet application has 5 methods Application has a single start
1
2
(1)Local Applets
2
3
<applet
codebase="tictactoe"
code="TicTacToe.class"
width=120
height=120>
</applet>
A remote applet is one that is located on another computer system .This computer system
may be located in the building next door or it may be on the other side of the world-it
makes no difference to your Java-compatible browser.
No matter where the remote applet is located, it's downloaded onto your computer via the
Internet. Your browser must, of course, be connected to the Internet at the time it needs to
display the remote applet.
To reference a remote applet in your Web page, you must know the applet's URL (where
it's located on the Web) and any attributes and parameters that you need to supply in
order to display the applet correctly
<applet
codebase="http://www.myconnect.com/applets/"
3
4
code="TicTacToe.class"
width=120
height=120>
</applet>
Life cycle of applet
An applet life cycle is a cycle which starts with opening of an html file or a web page in which
the applet is loaded.
The states of applet include:
1. Born or initialization state
2. Running state
3. Idle state
4
5
Initialization state: Applet enters born/initialization state when it is first loaded. This is done by
calling init( ) method of Applet class. The applet is born. At this stage, we may do the following,
if required.
Create objects needed by the applet
Set up colors
The initialization occurs only once in the applet’s life cycle. To provide any of the behaviors
mentioned above, we must override the init( ) method:
public void init( )
{
……………..
…………….. (Action)
……………..
}
Running state: Applet enters running state when the system calls the start( ) method of Applet
class. This occurs automatically after the applet is initialized. start( ) can be called more than
once but init( ) is called only once. We may override the start( ) method to create a thread to
control the applet.
public void start( )
{
……………..
…………….. (Action)
……………..
}
Idle state: An applet becomes idle when it is stopped from running. Stopping occurs
automatically when we leave the page containing the currently running applet. We can do so by
calling stop( ) method explicitly. If we use a thread to run the applet, then we must use stop( )
method to terminate the thread. We can achieve by overriding the stop( ) method:
public void stop( )
5
6
{
……………..
…………….. (Action)
……………..
}
Dead state: An applet is said to be dead when it is removed from memory. This occurs when we
invoke destroy( ) method or automatically enters dead state when we quit the browser. Like
initialization, destroying stage occurs only once in the applet’s life cycle. If the applet has
created any resources, like threads, we may override the destroy( ) method to clean up these
resources.
public void destroy( )
{
……………..
…………….. (Action)
……………..
}
Applet lifecycle methods
1) public void init( ): The init( ) method is the first method to be called. This is where you
should initialize variables. This method is called only once during the runtime of applet.
2) public void Start( ): The start method is called after init( ). It is called to restart an applet
after it has been stopped.
3) public void paint(Graphics g ):The paint method is called each time whenever your
applets output must be redrawn. For eg: the window in which the applet is running may
be overwritten by another. Window may be minimized and then restored. paint( ) is also
called when the applet begins execution. Whatever the cause, whenever the applet must
redraw its output, paint( ) is called. The paint( ) method has one parameter of type
Graphics. This parameter will contain the graphics context which describes the graphics
environment in which the applet is running.
4) public void stop( ): the stop method is called when a web browser leaves the HTML
documents containing the applet when it goes to another page.
6
7
5) public void destroy( ): The destroy method is called when the environment determines
that your applet needs to be removed completely from memory. When this is called
system will free up any resources the applet may be using, the stop( ) method is called
always before destroy( ).
7
8
ALIGN is an optional attribute that specifies the alignment of the applet. This attribute is treated
the same as the HTML IMG tag with these possible values: LEFT, RIGHT,TOP, BOTTOM,
MIDDLE, BASELINE, TEXTTOP, ABSMIDDLE, and ABSBOTTOM.
VSPACE and HSPACE
These attributes are optional. VSPACE specifies the space, in pixels,above and below the applet.
HSPACE specifies the space, in pixels, on each side of the applet. They’re treated the same as
the IMG tag’s VSPACE and HSPACE attributes.
PARAM NAME and VALUE
The PARAM tag allows you to specify applet-specific arguments in an HTML page. Applets
access their attributes with the getParameter( ) method.
Simple Example to Create an Applet Program
To create an Applet program follow the steps:
1.Create a Java file containing Applet Code and Methods described above
2.Create a HTML file and embed the .Class File of the Java file created in the first step
3.Run Applet using either of the following methods
Open the HTML file in java enabled web browser
Use AppletViewer tool(used only for testing purpose)
8
9
<applet code="app.class"width="400"
height="400">
<param name="name" value="this is for testing parameter">
</applet>
</body>
</html
import java.applet.Applet;
import java.awt.*;
g.drawString(str,50,20);
}
output
9
10
Applet methods
Method Description
void setBackground(Color To set background color applet window
colorname)
void setForeground(Color To set foreground color applet window
colorname)
Void showStatus(String str) To display status message in the status bar
Of applet.
Color getBackground( ) To obtain current background color
Color getForeground( ) To obtain current fore ground color
URL getDocumentBase() To obtain directory of the current Browser page
URL getCodeBase() To obtain directory from which the applets class file was
loaded
Advantage of applet
o It works at client side so less response time.
o Secured
10
11
Drawback of Applet
Important Questions .
11