0% found this document useful (0 votes)
63 views11 pages

Unit I

The document discusses applets and provides definitions and examples. It defines a Java applet as a small dynamic Java program that can be transferred via the Internet and run by a Java-compatible Web browser. It then differentiates between local and remote applets, explaining that local applets do not require an Internet connection while remote applets do. Next, it compares applets and applications, noting that applets are smaller programs that run in a browser while applications are larger programs that run on their own. It also explains the life cycle of an applet, describing the init(), start(), stop(), paint(), and destroy() methods. Finally, it outlines the <applet> tag and its attributes like code, codebase, width

Uploaded by

moaz.sheikh11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views11 pages

Unit I

The document discusses applets and provides definitions and examples. It defines a Java applet as a small dynamic Java program that can be transferred via the Internet and run by a Java-compatible Web browser. It then differentiates between local and remote applets, explaining that local applets do not require an Internet connection while remote applets do. Next, it compares applets and applications, noting that applets are smaller programs that run in a browser while applications are larger programs that run on their own. It also explains the life cycle of an applet, describing the init(), start(), stop(), paint(), and destroy() methods. Finally, it outlines the <applet> tag and its attributes like code, codebase, width

Uploaded by

moaz.sheikh11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

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.

Local & Remote Applet

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.

Specifying a Local Applet:

<applet codebase="path" code="NewApplet.class" width=120 height=120 ></applet>

Remote Applets:-

 A remote applet is developed by someone else and stored on a remote computer


connected to the Internet.
 Remote applet requires Internet connection to locate and load a remote applet.
 To access remote applet, we must know the applet’s URL (Uniform Resource Locator)
and must be specified in the CODEBASE attribute of applet Tag.

Specifying a Remote Applet:

<applet codebase=”http://www.myconnectdemo.com/applets/” code="NewApplet.class"


width=120 height=120> </applet>

Applet and Application


Que:
1. Give difference between applet and application.
2. Differentiate APPLET & Application.
3. What is an applet? How does applet differ from applications?
4. Describe the difference between applet and application.
Ans:
 Java program come in two ways:
1. Stand Alone applications
2. Applets
Applet Application
Small Program Large Program
Can be executed on stand alone
Used to run a program on client Browser
computer system
Applet is portable and can be executed by any Need JDK, JRE, JVM installed on client
JAVA supported browser. machine.
Applet applications are executed in a Application can access all the resources
Restricted Environment of the computer
Applications are created by writing
Applets are created by extending the
public static void main(String[] s)
java.applet.Applet
method.
Applet application has 5 methods which will
Application has a single start point
be automatically invoked on occurance of
which is main method
specific event
Example:
import java.awt.*;
import java.applet.*;

public class Myclass extends Applet public class MyClass


{ {
public void init() { } public static void main(String args[]) {}
public void start() { } }
public void stop() {}
public void destroy() {}
public void paint(Graphics g) {}
}
Life Cycle of Applet

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:

1. List attributes of <APPLET> tag.


2. Explain <applet> tag and its attributes.
3. Explain <applet> tag with its necessary attributes.
4. Applet tag
Ans:
 The Applet tag is used to start an applet from both an HTML document and from an
appletviewer.
 The syntax for the standard APPLET tag is shown here:

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

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 are 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.

Steps to create an Applet program


Que:
1. Describe process to design a webpage with <APPLET>tag.
Ans:
To create an Applet program
1. Create a Java file using any editor.
2. Add the Applet Code in the java file.
 All applets are subclasses of Applet Class. Thus, all applets must import
java.applet.Applet and extend Applet class.
 An applet must be declared as public class.
Example:
import java.applet.Applet;
public class First extends Applet{

 To compile applet write in command prompt following command:

c:\>javac First.java
 Once an applet has been compiled,.class file is generated.
 Include .class file in an HTML file using APPLET tag.

Adding an Applet to HTML file


 Create a HTML file and add applet tag.
 In code attribute of Applet tag, write name of .Class file created after compiling the
Applet file.
 Example:
<html>

<body>
<applet code=" DemoParam.class" width="300" height="300">
</applet>
</body>
</html>

To Run Applet

 There are two ways to run an applet

1. By HTML File (Using Java-Compatible Web Browser)


2. By appletViewer tool.

1. To Run Applet by html file:

 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;

public class First extends Applet{

public void paint(Graphics g){


g.drawString("welcome",150,150);
}
}

myapplet.html
<html>
<body>
<applet code="First.class" width="300" height="300">
</applet>
</body>
</html>

2. To Run Applet by appletviewer tool:

 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>
*/

public class First extends Applet{

public void paint(Graphics g){


g.drawString("welcome to applet",150,150);
}

 To execute the applet by appletviewer tool, write in command prompt:

c:\>javac First.java
c:\>appletviewer First.java

Passing Parameter to Applets


Que:
1. Explain < PARAM >tag in applet using example.
2. Explain how to pass parameters to applet: with syntax and suitable example.
3. How to pass parameter to java applet?
4. Explain parameter passing in Applet.
5. <PARAM> tag contains two attributes namely ________ and _______.
Ans:

 <PARAM..> tag is used to pass The "command-line" arguments to a Java applet from
HTML file.
 The format of the <PARAM> tag:

<PARAM NAME=x VALUE=y>

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.

<APPLET CODE=sample.class WIDTH=anInt HEIGHT=anInt>


<PARAM NAME=parameter1 VALUE=aValue>
<PARAM NAME=parameter2 VALUE=anotherValue>
</APPLET>
 To retrieve a parameter,the getParameter() method is used.
 It returns the value of the specified parameter in the form of a String object.
 Example:
DemoParam.java

import java.applet.Applet;
import java.awt.Graphics;

public class DemoParam extends Applet{

public void paint(Graphics g){

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:

o It works at client side so less response time.


o Secured
o It can be executed by browsers running under many plateforms, including Linux,
Windows, Mac Os etc.

Feature of Applets Over HTML

o Displaying dynamic web pages of a web application.


o Playing Sound files.
o Displaying Documents.
o Playing Animations.

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.
o An Applet cannot load libraries.
o Applet cannot read or write files on the execution host.

Component class
 The java.AWT.Component class provides 1 life cycle method of applet.

1. public void paint(Graphics g):


 It is used to paint the Applet.
 It provides Graphics class object that can be used for drawing oval, rectangle, arc
etc.

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

You might also like