0% found this document useful (0 votes)
21 views22 pages

Chap 2 Applet

Uploaded by

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

Chap 2 Applet

Uploaded by

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

Chapter 11: Applets

Applets are small programs that are used to prepare web applications and run over the internet. To start with
applet, first of all remember that Applet is a library class which comes under java.applet and we also need to import
java.awt packages. Remember that applets are not like console programs that executes with command based runtime
interpreter. Applets are programs specially designed for web applications and are executed using a web browser or
appletviewer.
An applet program do not have main( ) method to start execution but there are some applet methods which are
needed to be overridden in a user defined class that extends Applet class. Execution sequence of these applet methods
is fixed. We will study these all methods in applet’s life cycle. As applet do not concern with console programming,
hence the input and output statements we used till now are not applicable now onwards. Instead of that we will use
AWT components for that purpose. AWT stands for Abstract Window Toolkit.
Once an applet program is compiled, it is executed by using a web browser or appletviewer. To execute an
applet program we have to set up APPLET tag in the same program - as a comment or by providing a separate
HTML file. This APPLET tag is shown in following example.
<applet code = “applet_class_name.class” width = “width_in_pixels” height = “height_in_pixels”>
</applet>
As mentioned above, either place this tag in the same program as a multiline comment (/* …… */) or create a
separate HTML file containing this tag. Both the execution processes are discussed in this chapter.

11.1 Creating Applet

Q. How to create an applet? Explain with an example.

To create an applet first of all we have to extend a user defined class with library class Applet by using keyword
extends. For example,
class apl1 extends Applet
{
------------ // define applet methods here
------------
}
Here, the class apl1 is inheriting the library class Applet and hence has to override Applet class’ methods. The
Applet class contains actually 24 methods. We can override them in our class as per requirement. Some of them are
shortly discussed here.
void init( )
Will be executed when an applet begins its execution. This is the first method of an applet to be executed.
void start( )
This method will be executed automatically after init( ) method.
void paint(Graphics obj)
This method redraws the applet output. Generally used to draw graphical objects in the applet.
void stop( )
This method will be called by the browser to suspend the execution of the applet. Once an applet is stopped, it will
begin from start( ) method.
void destroy( )
Called after stop( ) to perform any cleanup before destruction of the applet.
String getAppletInfo( )
Returns a string containing information of applet.
URL getCodeBase( )
Returns the URL concern with the invoking applet.
boolean isActive( )
Returns true if applet is started, otherwise false.
void play(URL url)
plays an audio clip, if present at specified url.
void resize(int w, int h)
resizes the applet with specified width w and height h.

11.2 Applet Life Cycle

Q. Explain Applet Life Cycle with diagram.


Q. Explain the states of thread life cycle.
An applet, during its execution, goes through following stages.
• Initialization state
• Running state
• Idle state
• Dead / Destroyed state
Following Fig. 11.2.1 will show you the states and the series of changes in these states.

Fig. 11.2.1

Let us study these states one by one.


Initialization state : Applet executes init( ) method when it enters in initialization state. This init( ) method is
executed only once when applet is first time loaded. In init( ) method we can perform initial settings like creating
objects, connecting and opening databases, setting fonts and colors, initialize variables, etc.
Running state : Running state of an applet begins when start( ) method is executed. This method will be called
automatically after init( ) method and executes each time when applet’s contents are displayed. start( ) method
generally contains all executable and event related statements, handling databases and files, starting threads, playing
audio / video files, calling other methods, animations, etc.
Idle state : An applet is in idle state when it is stopped from running. Stopping occurs automatically when we leave
current running applet and jump to another applet. This can also be performed manually by using stop( ) method. stop(
) method is generally used to stop a thread, disconnecting database connections, stopping an audio / video clip,
animation, etc.
Destroyed state : An applet is in dead/destroyed state when it is completely removed from the memory. This occurs
when we quit the browser. This can be manually done by using destroy( ) method. This is the last method to be
executed for the applet. In destroy( ) method we release the used object or threads.
As we discussed earlier, an HTML file with <APPLET> tag is required for running an applet. Let us discuss the
steps involved to create and run an applet.
1. Create a .java file with a user defined applet class.
2. Compile this file and create its bytecode (.class file)
3. Design an HTML file containing <APPLET> tag
4. Run this .html file using appletviewer.
In apple life cycle we have discussed about applet states. It is also important to understand the methods which
are responsible for change in these states. Here is the brief discussion about the most useful methods.
init( ) : The init( ) method is the first method of applet to be executed. init( ) method will be executed only once, i.e.
first time when an applet is loaded. General form of init( ) method is:
void init( )
{
----------- // statements here
-----------
}
start( ) : This method will be called after init( ). This method will be executed each time when an applet is displayed.
General form of start( ) is:
void start( )
{
----------- // statements here
-----------
}
paint( ) : This method is used to draw graphical objects on applet page. This method will be executed whenever an
applet must redraw its output, paint( ) method has one parameter which is object of class Graphics. This object
provides graphical environment in which applet is running. Graphics class comes under java.awt package. The
definition of paint( ) is:
void paint(Graphics obj)
{
----------- // statements here
-----------
}
stop( ) : stop( ) method will be called when the browser leaves a page and jumps to another page. After stopping a
thread, using start( ) we can restart the execution. It begins execution from start( ) method instead of init( ). Definition
of stop( ) method is:
void stop( )
{
----------- // statements here
-----------
}
destroy( ) : The destroy( ) method will be executed when we exit from a browser or when browser environment
determines that your applet is no longer useful. Hence the destroy method is the last method to be executed for the
applet. This method is defined as:
void destroy( )
{
----------- // statements here
-----------
}
Here is a small program demonstrating applet’s skeleton. Call this program appl_demo.java

➢ Program 11.2.1 :

import java.awt.*;
import java.applet.*;
public class appl_demo extends Applet
{
public void init( )
{
// this method executes first and only once when applet is initially loaded.
}
public void start( )
{
// this method will be called second, after init( ) method and called each time.
}
public void paint(Graphics g)
{
// used to draw graphical objects using object g.
}
public void stop( )
{
// executes when applet is stopped or when controls leaves a page.
}
public void destroy( )
{
// executes after stop( ). This is the last method to be executed.
}
}

11.3 Displaying it using Web Browser with appletwiewer.exe


Q. Explain <applet> tag with its attributes.
Q. How to display an applet using <applet> tag.
We have discussed about Applet class, steps to be followed for applet programs, applet life cycle and its states,
<APPLET> tag. These all are required to understand applet programming. Now we will learn to execute applets using
appletviewer. appletviewer is a tool provided by JDK to execute an applet. Here is the HTML file containing
<APPLET> tag. This HTML file is linked with above program.
<html>
<body>
<applet code = “appl_demo.class” height = “300” width = “300”> </applet>
</body>
</html>
Let us call this file as one.html. This file is connected with “appl_demo.class” file using <APPLET> tag. This
tag has total 8 different attributes; out of them here we have used 3. They are
[CODE = “applet’s .class file”]
[HEIGHT = “height of appletviewer in pixel”]
[WIDTH = “width of appletviewer in pixel”]
Other attributes are: [CODEBASE, ALT, NAME, ALIGN, VSPACE, HSPACE]
Refer following screenshot and study the execution process.

Although, above program is not able to display any output contents, but still you can see this appletviewer
screen as output.

Note : Do not forget to make the applet class as public. If you forget it, then in the same appletviewer screen, you will see
“Applet not initialized” at the bottom.

11.4 The HTML APPLET Tag with all attributes.


Q. Explain <applet> tag with its attributes.
Let us call this file as one.html. This file is connected with “appl_demo.class” file using <APPLET> tag. This
tag has total 9 different attributes; out of them here we have already used 3. They are
CODE
A mandatory attribute that specifies the name of the file containing applet’s .class file that has to be loaded into the
web browser or appletviewer.

HEIGHT
A mandatory attribute that specifies the height of applet’s display area, in pixels.

WIDTH
A mandatory attribute that specifies the width of applet’s display area, in pixels.

CODEBASE
An optional attribute that specifies the base URL of the applet code, which is the directory that will be searched for the
applet’s executable class file.

ALT
An optional attribute that specifies a short alternate message that will be displayed if the web browser understands the
<applet> tag but cannot run Java applets, currently.

NAME
An optional attribute that is to specify an alternate name for the applet’s instance.

ALIGN
An optional attribute that specifies the alignment of applet.

VSPACE
An optional attribute that specifies vertical space i.e. to leave above and below the applet, in pixels.

HSPACE
An optional attribute that specifies vertical space i.e. to leave on both sides of the applet, in pixels.

11.5 Passing Parameters to applet


Q. Explain parameter passing to applet.
Q. How to pass parameters to applet using <param> tag.
We can pass user defined parameters to an applet using <PARAM> tag. (PARAM is a short form of
PARAMETER). This tag has two important attributes NAME and VALUE. Refer following syntax:

<PARAM NAME = “parameter name” VALUE = “value of parameter”>

This <PARAM> tag must be inside <APPLET> tag. This passed parameter is received in Java program by using
getParameter( ) method. This method is defined as:

String getParameter(String parameter_name)


This parameter_name represents the parameter name passed from applet and the value of that parameter will fetched
from applet and returned as string. Following example demonstrates use of <PARAM> tag and getParameter( )
method.
Following program shows the use of getParameter( ). Call this program as param_demo.java

➢ Program 11.5.1

import java.awt.*;
import java.applet.*;
public class param_demo extends Applet
{
String st;
public void init()
{
st=getParameter("argument");
st = "Hello " + st +"!";
}
public void paint(Graphics g)
{
g.drawString(st,100,100);
}
}
Now, lets create HTML file containing <PARAM> tag.
Call this file as two.html
<html>
<head>
<title>PARAM tag demo</title>
</head>
<body>
<applet code="param_demo.class" width="300" height="300">
<param name = "argument" value = "James">
</applet>
</body>
</html>
After executing above html file using appletviewer we can get following output.
11.6 Event handling in applet
Event handling is Applet also supports all the event classes and event listener interfaces that are shown in section
10.9. Before going through following programs that demonstrates event handling in Applets, first go through the
Table 10.9.1, Table 10.9.2 and Table 10.9.3.
We can easily implement all the example programs of section 10.9.5 (that are demonstrating event handling in
Frame) into Applet.

11.6.1 Implementing MouseListener in Applet


To begin the examples of event handling, the MouseListener and TextListener are good choice to start with. Let’s
begin with the first MouseListener example that generates on each mouse action. Here is a program to demonstrate
MouseListener to respond to mouse events.

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

/*
<applet code= “mouse _demo” width= “400” height= “700” >
</applet>
*/

public class mouse_demo extends Applet implements MouseListener, MouseMotionListener


{
public String st="";
public void init( )
{
addMouseListener(this); //registering MouseListener for whole applet
}
public void mouseClicked(MouseEvent me)
{
st="Mouse is clicked";
repaint( );
}
public void mouseEntered(MouseEvent me)
{
st="Mouse Entered";
repaint( );
}
public void mouseExited(MouseEvent me)
{
st="Mouse Exited";
repaint( );
}
public void mousePressed(MouseEvent me)
{
st="Mouse Button Pressed";
repaint( );
}
public void mouseReleased(MouseEvent me)
{
st="Mouse Button Released";
repaint( );
}
public void mouseDragged(MouseEvent me)
{
st= “Mouse Dragged”;
repaint( );
}
public void mouseMoved(MouseEvent me)
{
st= “Mouse Moved”;
repaint( );
}
public void paint(Graphics g)
{
g.drawString(st,20,20);
}
}

Here, MouseListener interface is implemented to handle mouse related events and all its methods are overridden in
implementing class mouse_demo. It is registered for events by using addMouseListener( ) method. Each time when
an event occurs, value of String changes and repaint( ) method is used to invoke paint( ) method manually,
whenever required. Whenever mouse event is performed, the MouseListener recognize the type of event and call to
related method. Here is the output of above program.

11.6.2 Implementing KeyListener on Applet


Here is a program demonstrating KeyListener, that handles keyPressed( ), keyReleased( ) and keyTyped( ) events.

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

/*
<applet code= “key _demo” width= “400” height= “700” >
</applet>
*/

public class key_demo extends Applet implements KeyListener


{
String st = "";
public void init( )
{
addKeyListener(this); // registering KeyListener for the whole applet
}
public void keyPressed(KeyEvent ke)
{
showStatus("Key Down");
}
public void keyReleased(KeyEvent ke)
{
showStatus("Key Up");
}
public void keyTyped(KeyEvent ke)
{
st = "You typed : " + ke.getKeyChar( );
repaint( );
}
public void paint(Graphics g)
{
g.drawString(st,20,20);
}
}

Here, KeyListener interface is implemented to handle Key related events. It is registered by using addKeyListener( )
method. The showStatus( ) method is used to display any user defined message on the status bar. The getKeyChar( )
method returns the character which is pressed for performing that event. Here is the output of above program.

11.6.3 Displaying mouse position when mouse is moved


Following is another program that will display mouse position in status bar. The mouse position should automatically
change, whenever mouse is moved.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class mouse_drag_move extends Applet implements MouseMotionListener
{
String msg = "";
int mouseX = 0, mouseY = 0; // coordinates of mouse
public void init()
{
addMouseMotionListener(this);
}

// Handle mouse dragged.


public void mouseDragged(MouseEvent me)
{
// save coordinates
mouseX = me.getX( );
mouseY = me.getY( );
msg = "*";
showStatus("Dragging mouse at " + mouseX + ", " + mouseY);
repaint( );
}

// Handle mouse moved.


public void mouseMoved(MouseEvent me)
{
// show status
showStatus("Moving mouse at " + me.getX( ) + ", " + me.getY( ));
}
// Display msg in applet window at current X,Y location.
public void paint(Graphics g)
{
g.drawString(msg, mouseX, mouseY);
}
}

The getX( ) and getY( ) methods retrieves the position of mouse pointer on X and Y axis, respectively. Refer following
output.

11.6.4 Demonstrating event on List


Here is a program performing event on List when its selection is changed.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/*
<applet code="ListDemo" width=300 height=180>
</applet>
*/

public class ListDemo extends Applet implements ItemListener


{
List os, browser;
Label lb1,lb2;
public void start()
{
setLayout(null);
os = new List(4, false);
browser = new List(4, false);
lb1 = new Label("Operating System is : ");
lb2 = new Label("Browse is : ");
os.add("Windows 98/XP");
os.add("Windows NT/2000");
os.add("Solaris");
os.add("MacOS");
os.select(0);
browser.add("Netscape 3.x");
browser.add("Netscape 4.x");
browser.add("Netscape 5.x");
browser.add("Netscape 6.x");
browser.add("Internet Explorer 4.0");
browser.add("Internet Explorer 5.0");
browser.add("Internet Explorer 6.0");
browser.add("Lynx 2.4");
browser.select(0);
add(os);
add(browser);
add(lb1);
add(lb2);
os.addItemListener(this);
browser.addItemListener(this);
os.setBounds(20,20,150,100);
browser.setBounds(190,20,150,100);
lb1.setBounds(50,150,200,30);
lb2.setBounds(50,190,200,30);
}

public void itemStateChanged(ItemEvent ie)


{
lb1.setText("Operating System is : " + os.getSelectedItem());
lb2.setText("Browser is : " + browser.getSelectedItem());
}
}

11.6.5 Demonstrating event on Checkbox using ItemListener


Here is a program that performs event on check or uncheck operation of three checkboxes and updates the label
accordingly.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

/*
<applet code="checkbox_event" width="400" height="400">
</applet>
*/

public class checkbox_event extends Applet implements ItemListener


{
Label lb1,lb2,lb3;
Checkbox ch1,ch2,ch3;
public void start( )
{
setLayout(null);
ch1 = new Checkbox("Option 1");
ch2 = new Checkbox("Option 2");
ch3 = new Checkbox("Option 3");
lb1 = new Label("Option 1 is : false");
lb2 = new Label("Option 2 is : false");
lb3 = new Label("Option 3 is : false");
add(ch1);add(ch2);add(ch3);
add(lb1);add(lb2);add(lb3);
ch1.setBounds(50,50,100,30);
ch2.setBounds(160,50,100,30);
ch3.setBounds(270,50,100,30);
lb1.setBounds(150,100,100,30);
lb2.setBounds(150,130,100,30);
lb3.setBounds(150,160,100,30);
ch1.addItemListener(this); // registering ch1 for ItemListener
ch2.addItemListener(this); // registering ch2 for ItemListener
ch3.addItemListener(this); // registering ch3 for ItemListener
}

public void itemStateChanged(ItemEvent ie)


{
lb1.setText("Option 1 is : " + ch1.getState( ));
lb2.setText("Option 2 is : " + ch2.getState( ));
lb3.setText("Option 3 is : " + ch3.getState( ));
}
}

Three Checkboxes are registered for ItemEvent and hence on each change of state of each checkboxes the method
itemStateChanged( ) gets invoked and we update the labels according to state of checkbox. Following is the output
of above program.
➢ Program 11.6.6 : an applet to display scrolling text from right to left in an applet window.

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

/*
<applet code="ScrollText" height="100" width="350">
</applet>
*/

public class ScrollText extends Applet implements Runnable


{
String banner="Tuhin Scrolling ";
int state;
boolean stopflag;

public void init( )


{
setBackground(Color.black);
setForeground(Color.green);
}

public void start( )


{
Thread t=new Thread(this);
stopflag=true;
t.start( );
}

public void run( )


{
char ch;
try
{
while(true)
{
repaint( );
Thread.sleep(150);
ch=banner.charAt(0);
banner = banner.substring(1,banner.length( ));
banner +=ch;
}
}catch (InterruptedException e)
{
System.out.println(e);
}
}

public void stop( )


{
stopflag=false;
Thread t=null;
}

public void paint(Graphics g)


{
Font a = new Font("Impact",Font.BOLD,45);
g.setFont(a);
g.drawString(banner,10,60);
}
}

1.7 Applet’s Graphics mehthods

Q. Explain following Applet’s graphics methods.


i) drawLine( ) ii)drawOval( ) iii)drawArc iv)fillRect( ) v)fillOval( )
We can write java applets that can draw lines, different graphical objects, images and text contents in different fonts,
colors and sizes. Every applet has its own area of the screen where these objects are drawn, it is called as
canvas. First of all see following Fig. 11.7.1 showing coordinate system of Java’s applet.

Fig. 11.7.1 : Co-ordinate system of output screen

Above Fig 11.7.1, shows that (0,0) coordinates are in top left corner of your screen. Here are some of the methods
required to study.
Remember that, all these methods are member functions of library class Graphics and draw them in paint( ) methods
of applet class.

11.7.1 Displaying String


A string can also be displayed on the screen as a graphical object. It is done by using drawstring( ) method. General
form of this method is :
void drawString(“String message” , int x , int y)
This method displays string message starting from specified x and y coordinates. Following program demonstrates use
of this method.

➢ Program 11.7.1 :

import java.awt.*;
import java.applet.*;
/ * <applet code = “string_demo.class” height = “300” width = “300”> </applet> */
public class string_demo extends Applet
{
public void paint(Graphics g)
{
g.drawString("String demo",60,100);
}
}
Output of above applet will be :

11.7.2 Drawing Line


Line is the simplest drawing shape that we can draw using graphical methods. The method drawLine( ) is used for
drawing a straight line.
The general form of drawLine( ) is:
void drawLine(int startX, int startY, int endX, int endY)
This method will draw a line that begins at startX – startY and ends at endX – endY by using current drawing color.
For example,
g.drawLine(40 , 75 , 110 , 190);

➢ Program 11.7.2 : Program to draws several lines in different colors

import java.awt.*;
import java.applet.*;
/ * <applet code = “line_demo.class” height = “300” width = “300”> </applet> */
public class line_demo extends Applet
{
public void paint(Graphics g)
{
g.drawLine(40,75,110,190);
g.setColor(Color.red);
g.drawLine(140,100,200,290);
g.setColor(Color.blue);
g.drawLine(25,65,150,210);
g.setColor(Color.green);
g.drawLine(70,90,130,135);
g.setColor(Color.yellow);
g.drawLine(95,125,175,230);
}
}
Above program will show following output in appletviewer.

11.7.3 Drawing Rectangles


We can also draw rectangles using drawing methods. There are four methods related to drawing a rectangle:
drawRect( ), fillRect( ), drawRoundRect( ), fillRoundRect( ).
drawRect( ) and fillRect( ) methods draws an outline and fills rectangle using current drawing color,
respectively.
Their general form is :
void drawRect(int startX, int startY, int width, int height)
void fillRect(int startX, int startY, int width, int height)
These methods create a starting point at startX – startY and dimensions are specified by width and height.
Another methods are drawRoundRect( ) and fillRoundRect( ). These methods are same as above two but they just
require two additional arguments. Their general forms are:
void drawRoundRect(int startX, int startY, int width, int height, int Xdiam, int Ydiam)
void fillRoundRect(int startX, int startY, int width, int height, int Xdiam, int Ydiam)

➢ Program 11.7.3 : Program to demonstrating rectangle methods

import java.awt.*;
import java.applet.*;
/ * <applet code = “rect_demo.class” height = “300” width = “300”> </applet> */
public class rect_demo extends Applet
{
public void paint(Graphics g)
{
g.drawRect(10, 10, 60, 50);
g.fillRect(110, 90, 40, 30);
g.drawRoundRect(190, 10, 60, 50, 15, 15);
g.fillRoundRect(60, 170, 140, 100, 30, 40);
}
}
Above program will show following output.

Tip : To draw a perfect rectangle, keep the width and height equal.

11.7.4 Drawing Ellipses and Circles

To draw ellipses and circles we have drawOval( ) and fillOval( ) mehods. The general form of these methods are:
void drawoval(int startX, int startY, int width, int height)
void filloval(int startX, int startY, int width, int height)
The general syntax of drawing oval is same as of drawing rectangle.
These methods, first generate a bounding rectangle and join midpoint points of each side and draw an oval.

➢ Program 11.7.4 :Program to demonstrates drawing & filling ellipses and circles with different colors

import java.awt.*;
import java.applet.*;
/ * <applet code = “oval_demo.class” height = “300” width = “300”> </applet> */
public class oval_demo extends Applet
{
public void paint(Graphics g)
{
g.drawoval(10, 10, 60, 60);
g.filloval(110, 90, 40, 40);
g.drawoval(190, 10, 60, 60);
g.filloval(60, 170, 140, 60);
}
}
After successful compilation and execution using appletviewer, output of above program will be :

Tip : To draw a perfect circle, keep the width and height equal.

11.7.5 Drawing Arcs

Arcs can also be drawn and filled by using graphical methods. General syntax to draw and fill arc is :
void drawArc(int startX, int startY, int width, int height, int startAngle, int sweepAngle)
void fillArc(int startX, int startY, int width, int height, int startAngle, int sweepAngle)
These methods also generates a bounding rectangle and the arc is drawn from startAngle through the angular distance
specified by sweepAngle. We specify the angles in terms of degree. The negative angle draws arc clockwise
and positive angle draws arc counterclockwise.

➢ Program 11.7.5 : Program to draws several arcs

import java.awt.*;
import java.applet.*;
/ * <applet code = “arc_demo.class” height = “300” width = “300”> </applet> */
public class arc_demo extends Applet
{
public void paint(Graphics g)
{
g.drawArc(10, 40, 70, 70, 0, 75);
g.setColor(Color.cyan);
g.fillArc(100, 40, 70, 70, 0, 75);
g.setColor(Color.pink);
g.drawArc(10, 100, 70, 80, 0, 175);
g.setColor(Color.black);
g.fillArc(100, 100, 70, 90, 0, 270);
g.setColor(Color.magenta);
g.drawArc(200, 80, 80, 80, 0, 180);
}
}
Above applet program will show following applet output.

11.7.6 Drawing Polygons


Java also provides methods to draw and fill polygons using drawPolygon( ) and fillPolygon( ) respectively General
form of these methods are:
void drawPolygon(int x[ ], int y[ ], int numpoints)
void fillPolygon(int x[ ], int y[ ], int numpoints)
Polygon’s endpoints are specified by providing arrays for x and y coordinates along with specifying the numbers of
points defined by x and y array.

➢ Program 11.7.6 : Program for drawing polygons

import java.awt.*;
import java.applet.*;
/ * <applet code = “poly_demo.class” height = “300” width = “300”> </applet> */
public class poly_demo extends Applet
{
public void paint(Graphics g)
{
int x1[]={30,200,30};
int y1[]={30,30,200};
int x2[]={200,200,30};
int y2[]={30,200,200};
g.drawPolygon(x1,y1,3);
g.fillPolygon(x2,y2,3);
}
}
Above program will produce following output.

11.8 Advantages and Disadvantages of Applet Vs Applications

Q. What are advantages and disadvantages of Applet?


Q. Differentiate between Applet and Application.
All the Java programs can be distributed between applet and application. Technically speaking, there are a few key
differences between them. Following table is to summarize them.

Applet Application
Needs no explicit installation on local machine. Can be Stand Alone, doesn’t need web-browser.
transferred through Internet on to the local machine and
may run as part of web-browser.
Execution starts with init() method. Does work without Execution starts with main( ) method.
main( ) method. Doesn’t work if main( ) method is not there.
Must run within a GUI (Using AWT or Swing May or may not be a GUI.
components).
Requires high security, as they are untrusted. Does not require any security, as they are
installed on the same machine.

From above differentiation, we cannot say that applets more valuable then applications. Both of these have their own
advantages and disadvantages over each other. But truly speaking, if I suggest you to choose one, between them, then
we will definitely prefer applets. It is because they are centrally managed and their upgradations are easy. Even they
have a centralized database; hence easy to access from anywhere in the world. Let’s List out the important advantages
and limitations of applets over application.

Advantages of applet
• Execution of applet does not require any installation or deployment procedure.
• Centrally managed, hence easy to upgrade and access.
• Platform independent.
• Writing and displaying animations and graphics are very much easy.
• Databases are centrally stored, hence can be accessed from anywhere.

Limitations of applet
• Each time during the real time environment, the Bytecodes of applet are to be downloaded from server. Hence
requires internet data.
• Applets are treated as untrusted therefore requires high security.

You might also like