Unit I
Unit I
Applet
Definition:
A Java applet is a small dynamic Java program that can be transferred via the Internet and
run by a Java-compatible Web browser.
Que:
1. Differentiate between Local & Remote Applet.
2. Give difference between Local and Remote Applet.
3. Explain local applet and remote applet.
Ans:
Local Applets:-
An applet developed locally and stored in a local system is known as a local applet.
Local applet does not require the Internet connection for execution.
It searches the directories in the local system and locates and loads the specified applet.
Remote Applets:-
Que:
1. Explain life cycle of an applet.
2. Explain life cycle of applet.
3. Write a Short note on Applet life cycle.
4. What are the Applet’s Life Cycle methods? Explain them?
Ans:
The life cycle of an applet is as shown in the figure below:
As shown in the above diagram, the life cycle of an applet starts with init()method and ends
with destroy() method.
init() and destroy() methods are executed only once in the applet life cycle while start(),
stop() and paint() methods are executed multiple times.
Applet life cycle methods:
1. init():
The init() method is the first method to execute when the applet is executed.
Variable declaration and initialization operations are performed in this method.
2. start():
The start() method contains the actual code of the applet that should run.
The start() method executes immediately after the init() method.
It also executes whenever the applet is restored, maximized or moving from one tab to
another tab in the browser.
3. paint():
The paint() method is used to redraw the output on the applet display area.
The paint() method executes after the execution of start() method and whenever the
applet or browser is resized.
This method takes a java.awt.Graphics object as parameter.
4. stop():
The stop() method stops the execution of the applet.
The stop() method executes when the applet is minimized or when moving from one tab
to another in the browser.
5. destroy():
The destroy() method executes when the applet window is closed or when the tab
containing the webpage is closed.
The destroy() method removes the applet object 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( ).
<APPLET> Tag
Que:
< APPLET
[CODEBASE = codebaseURL]
CODE = appletFile
[ALT = alternateText]
[NAME = appletInstanceName]
WIDTH = pixels
HEIGHT = pixels
[ALIGN = alignment]
[VSPACE = pixels]
[HSPACE = pixels]
>
[< PARAM NAME = appletParameter1 VALUE = value >]
[< PARAM NAME = appletParameter2 VALUE = value >]
. . .
[HTML Displayed in the absence of Java]
</APPLET>
Applet Tag Attributes:
CODEBASE
This optional attribute specifies the base URL of the applet -- the directory or folder that
contains the applet's code.
If this attribute is not specified, then the document's URL is used.
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.
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.
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
ALIGN is an optional attribute that specifies the alignment of the applet.
This attribute is treated the same as the HTML IMG tag.
Possible values can be: LEFT, RIGHT,TOP, BOTTOM, MIDDLE, BASELINE,
TEXTTOP, ABSMIDDLE, and ABSBOTTOM.
c:\>javac First.java
Once an applet has been compiled,.class file is generated.
Include .class file in an HTML file using APPLET tag.
<body>
<applet code=" DemoParam.class" width="300" height="300">
</applet>
</body>
</html>
To Run Applet
To execute the applet by html file, create an applet and compile it.
After that create an html file and place the applet code in html file. Now click the html
file.
First.java
import java.applet.Applet;
import java.awt.Graphics;
myapplet.html
<html>
<body>
<applet code="First.class" width="300" height="300">
</applet>
</body>
</html>
To execute the applet by appletviewer tool, create an applet that contains applet tag in
comment and compile it.
First.java
import java.applet.Applet;
import java.awt.Graphics;
/*
<applet code="First.class" width="300" height="300">
</applet>
*/
c:\>javac First.java
c:\>appletviewer First.java
<PARAM..> tag is used to pass The "command-line" arguments to a Java applet from
HTML file.
The format of the <PARAM> tag:
Two attributes:
1. NAME specifies the name of the argument
2. VALUE specifies the value for this argument.
<PARAM> tags must appear between the <APPLET> and </APPLET> tags.
Example of the <PARAM> tag.
import java.applet.Applet;
import java.awt.Graphics;
String str=getParameter("msg");
g.drawString(str,50, 50);
}
myapplet.html
<html>
<body>
<applet code=" DemoParam.class" width="300" height="300">
<param name="msg" value="Welcome to applet">
</applet>
</body>
</html>
Advantage of Applet
Que:
1. Discuss the advantage and limitation of Applet.
Ans:
Drawback of Applet
o Plugin is required at client browser to execute applet.
Component class
The java.AWT.Component class provides 1 life cycle method of applet.
Applet methods
Method Description
void drawString( string , x , y ) To display a string at x,y coordinate in
the applet.
Void drawLine( x1 , y1 , x2 , y2 ) Draws a line connecting two Point.
Void drawOval( left , top , width , height ) Draws a circle or an oval that fits within
the rectangle specified by the left, top,
width and height arguments.
Void fillOval( left , top , width , height ) used to fill oval with the default color.
Void drawRect( left , top , width , height ) Draws draw Rectangle that fits within the
rectangle specified by the left, top, width
and height arguments.
Void fillRect( left , top , width , height ) used to fill Rectangle with the default
color.
Void drawRoundRect( left , top , width , Draws drawRoundRect that fits within
height,arcWidth,arcHeight ) the rectangle specified by the left, top,
width and height arguments.
Void fillRoundRect(left , top , width , height, used to fill Round Rectangle with the
arcWidth,arcHeight) default color.
Void drawArc( left , top , width , height ,startAngle , Draws an arc representing a portion of an
arcAngle ) ellipse.
void setBackground(Color colorname) To set background color of applet
window
void setForeground(Color colorname) To set foreground color of applet
window
Void setColor(Color colorname) To set color of applet component.
Color getBackground( ) To obtain current background color
Color getForeground( ) To obtain current foreground color
Color getColor(Color colorname) To obtain current color.
Void showStatus(String str) To display status message in the status
bar of applet.
URL getDocumentBase() To obtain directory of the current
Browser page.
URL getCodeBase() To obtain directory from which the
applets class file was loaded.
Color Constants :
We can use the static color variables of Color classes to set color of applet components.
Color.black Color.lightGray
Color.blue Color.magenta
Color.cyan Color.orange
Color.darkGray Color.pink
Color.gray Color.red
Color.green Color.white
Color.yellow