UNIT-I Applet

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

1

UNIT –I

JAVA APPLET

Introduction to applet or what is applet


 An applet is a Java program that is referenced by a Web page and runs inside a Java-
enabled Web browser.
 An applet begins execution when an HTML page that "contains" it is loaded.
 Either a Java-enabled Web browser or the appletviewer is required to run an applet
<APPLET CODE=ImgJump WIDTH=300 HEIGHT=200>
</APPLET>

Difference between applet and application

Java program come in two ways:


1) Stand alone applications which do not require any browser to ender its output.

2) Applets which can be embedded in web pages.

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

which will be automatically invoked on point which is main method


occurance of specific event
Applications are created by
Applets are created by extending the
writing public static void
java.applet.Applet
main(String[] s) method.
Example:
import java.awt.*;
import java.applet.*;

public class MyClass


public class Myclass extends Applet
{
{
public static void main(String
public void init() { }
args[]) {}
public void start() { }
}
public void stop() {}
public void destroy() {}
public void paint(Graphics g) {}
}

Local and Remote Applets

Basically Web pages can contain two types of applets:

(1)Local Applets

 A local applet is one that is stored on your own computer system


 When Web page must find a local applet, it doesn't need to retrieve information from the
Internet-in fact, your browser doesn't even need to be connected to the Internet at that
time. As you can see in below code , a local applet is specified by a path name and a file
name.

2
3

<applet

codebase="tictactoe"

code="TicTacToe.class"

width=120

height=120>

</applet>

(2) Remote Applets

 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

Specifiying a Remote Applet.

<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. dead or destroyed 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 initial values

Load images or fonts

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( ).

The <APPLET> Tag


CODEBASE
CODEBASE is an optional attribute that specifies the base URL of the appletcode, which is the
directory that will be searched for the applet’s executable class file (specified by the CODE tag).
The HTML document’s URL directory is used as the CODEBASE if this attribute is not
specified. The CODEBASE does not have to be on the host from whichthe HTML document was
read.
CODE
CODE is a required attribute that gives the name of the file containing your applet’s compiled
.class file. This file is relative to the code base URL of the applet, which is thedirectory that the
HTML file was in or the directory indicated by CODEBASE if set.
ALT
The ALT tag is an optional attribute used to specify a short text message that should be displayed
if the browser recognizes the APPLET tag but can’t currently run Java applets. This is distinct
from the alternate HTML you provide for browsers that don’t support applets.
NAME
NAME is an optional attribute used to specify a name for the applet instance.Applets must be
named in order for other applets on the same page to find them by name and communicate with
them. To obtain an applet by name, use getApplet( ), which is defined by the AppletContext
interface.
WIDTH and HEIGHT
WIDTH and HEIGHT are required attributes that give the size (in pixels) of the applet display
area.
ALIGN

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)

Passing Parameter to Applets


<PARAM..> tag is used to pass the parameter value from HTML file to Applet code. In
theexample shown below parameter which is used is named as “name” and value of the
parameter is initialized as “this is for testing parameter”. Now in the Applet code, you should use
same parameter name as “name” to fetch the value.

Code for para.html


<html>
<head>
<title>HTML applet Tag</title>
</head>
<body>

8
9

<applet code="app.class"width="400"

height="400">
<param name="name" value="this is for testing parameter">

</applet>
</body>
</html

Code for para.java

import java.applet.Applet;
import java.awt.*;

public class app extends Applet{

public void paint(Graphics g){


String str=getParameter("name");

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

o It can be executed by browsers running under many platforms, including Linux,


Windows, Mac Os etc.

10
11

Drawback of Applet

o Plugin is required at client browser to execute applet.


o Applets is Slow to Download
o There is restriction in inter applet communication

Java.awt Methods for drawing


_ g.drawLine( x1 , y1 , x2 , y2 );
_ g.drawOval( left , top , width , height );
_ g.fi llOval( left , top , width , height );
_ g.drawRoundRect( left , top , width , height );
_ g.fi llRoundRect( left , top , width , height );
_ g.drawArc( left , top , width , height ,
startAngle , arcAngle );
_ g.drawString( string , x , y );

Important Questions .

1 Explain <applet> tag and its atributes

2 What is the use of appletviewer utility inJAVA


Give difference between Local and Remote applet
3

4 Write a program in JAVA to draw five red circles in the applet


What are the Applet’s Life Cycle methods? Explain them?
5

6 Explain < PARAM >tag in applet using example


Develop an applet that receive two numeric values as input from the user and display the
7
addition of these two nos.
8 Explain < PARAM >tag in applet using example
Develop an applet that receive two numeric values as input from the user and display the
9
addition of these two nos.

11

You might also like