Module6 Applets
Module6 Applets
Module 6
Applets
The goal for HTML was to create a platform-independent language for constructing
hypertext documents to communicate multimedia information easily over the Internet.
Using an Internet protocol called Hyper-Text Transport Protocol (HTTP), HTML
documents could be transmitted to any user on the Internet and displayed by software
called a browser.
<HTML>
<HEAD>
<TITLE>The Title</TITLE>
</HEAD>
</HTML>
As the name suggests, HTTP is a protocol used for transferring hypertext documents
across the Web. A protocol is a specification for communicating information URLs
URL stands for Uniform Resource Locator. The word Uniform in this case means that the
syntax follows a standard convention, even when extended into new addresses. Resource
means anything that has an identity, such as a file, a program, an image, and so forth. A
URL includes the following components:
• http://—The http refers to the overall protocol being used, and following that must be
the colon and two forward slashes.
• The name of the host computer on which the resource resides. Typically this is the
domain name (for example, www.mu.ac.in).
• The port on which the Web server is listening—this is typically 80, and must be
preceded by a colon (:). This is usually omitted and assumed to be :80. Ports are logical
addresses to which messages may be sent in an operating system.
• The absolute path, preceded by a forward slash (/), to the folder where the file is
located, including the filename.
For example, a typical URL might be www.e4free.com/asubfolder/afile.htm.
• If there is a query attached to the URL, it begins with a question mark, and the
name/value pairs in the query are separated by ampersands.
Applet Fundamentals
Applets are small applications that are accessed on an Internet server, transported over
the Internet, automatically installed, and run as part of a Web document.
Applets interact with the user through the AWT, not through the console-based I/O
classes. The AWT contains support for a window-based, graphical interface.
Every applet that is being created must be a subclass of Applet.
1
Notes by – Mandar Sohani
CO6: Develop web based applications.
ES05-Object Oriented Programming FE SEM II Mandar Sohani
An applet viewer executes the applet in a new separate window. This is generally the
fastest and easiest way to test the applet.
<HTML>
<HEAD>
<TITLE>The Title</TITLE>
<applet code="SimpleApplet" width=200 height=60>
</applet>
</HEAD>
</HTML>
The width and height statements specify the dimensions of the display area used by the
applet.
C:\>appletviewer RunApp.html
OR
Include the APPLET tag as a comment at the beginning of Java source code file
1. Edit a Java source file.
2. Compile the program.
3. Execute the applet viewer, specifying the name of the applet’s source file.
The applet viewer will encounter the APPLET tag within the comment and execute
the applet.
import java.awt.*;
import java.applet.*;
/*
<applet code="SimpleApplet" width=200 height=60>
</applet>
*/
public class SimpleApplet extends Applet
2
Notes by – Mandar Sohani
CO6: Develop web based applications.
ES05-Object Oriented Programming FE SEM II Mandar Sohani
{
public void paint(Graphics g)
{
g.drawString("A Java Applet Programming", 20, 20);
}
}
All applets are subclasses of Applet. Thus, all applets must import java.applet.
Applets must also import java.awt.
Execution of an applet does not begin at main( ). Actually, few applets even have
main( ) methods.
CODEBASE:
CODEBASE is 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 (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
which the HTML document was read.
CODE:
CODE is a required attribute that gives the name of the file containing the applet’s
compiled .class file. This file is relative to the code base URL of the applet, which is the
directory 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 understands 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.
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 with these possible values:
LEFT, RIGHT, TOP, BOTTOM, MIDDLE, BASELINE, TEXTTOP, ABSMIDDLE,
and ABSBOTTOM.
import java.awt.*;
import java.applet.*;
/*
<applet code="ParamDemo" width=300 height=80>
<param name=fontName value=Courier>
<param name=fontSize value=14>
</applet>
*/
public class ParamDemo extends Applet
{
String fontName;
int fontSize;
public void start()
{
String param;
fontName = getParameter("fontName");
if(fontName == null)
fontName = "Not Found";
param = getParameter("fontSize");
try
{
if(param != null)
fontSize = Integer.parseInt(param);
4
Notes by – Mandar Sohani
CO6: Develop web based applications.
ES05-Object Oriented Programming FE SEM II Mandar Sohani
else
fontSize = 0;
}
catch(NumberFormatException e)
{
fontSize = -1;
}
public void paint(Graphics g)
{
g.drawString("Font name: " + fontName, 0, 10);
g.drawString("Font size: " + fontSize, 0, 26);
}
}
Returns true if the applet has been started. It returns false if the applet has been
stopped.
10. void play(URL url)
If an audio clip is found at the location specified by url, the clip is played.
11. void play(URL url, String clipName)
If an audio clip is found at the location specified by url with the name specified
by clipName, the clip is played.
11. void start( )
Called by the browser when an applet should start (or resume) execution. It is
automatically called after init( ) when an applet first begins.
12. void stop( )
Called by the browser to suspend execution of the applet. Once stopped, an applet is
restarted when the browser calls start( ).
6
Notes by – Mandar Sohani
CO6: Develop web based applications.
ES05-Object Oriented Programming FE SEM II Mandar Sohani
init( )
The init( ) method is the first method to be called. This method is can be used to
initialize variables. This method is called only once during the run time of the applet.
start( )
The start( ) method is called after init( ). It is also called to restart an applet after it has
been stopped. Whereas init( ) is called once—the first time an applet is loaded—start( ) is
called each time an applet’s HTML document is displayed onscreen. So, if a user leaves a
web page and comes back, the applet resumes execution at start( ).
paint( )
The paint( ) method is called each time the applet’s output must be redrawn. This
situation can occur for several reasons. For example, the window in which the applet is
running may be overwritten by another window and then uncovered. Or the applet
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. This context is used whenever output to the applet is required.
stop( )
The stop( ) method is called when a web browser leaves the HTML document containing
the applet—when it goes to another page, for example. When stop( ) is called, the applet
is probably running.
destroy( )
The destroy( ) method is called when the environment determines that the applet needs to
be removed completely from memory. It should be used to free up any resources the
applet may be using. The stop( ) method is always called before destroy( ).
The Graphics class defines a number of drawing functions. Each shape can be drawn
edge-only or filled. Objects are drawn and filled in the currently selected graphics color,
which is black by default. When a graphics object is drawn that exceeds the dimensions
of the window, output is automatically clipped.
7
Notes by – Mandar Sohani
CO6: Develop web based applications.
ES05-Object Oriented Programming FE SEM II Mandar Sohani
Drawing Lines:
Lines are drawn by means of the drawLine( ) method, shown here:
Drawing Rectangles:
The drawRect( ) and fillRect( ) methods display an outlined and filled rectangle,
respectively. They are shown here:
import java.awt.*;
import java.applet.*;
/*
<applet code="Rectangles" width=300 height=200>
</applet>
*/
public class Rectangles extends Applet
8
Notes by – Mandar Sohani
CO6: Develop web based applications.
ES05-Object Oriented Programming FE SEM II Mandar Sohani
{
public void paint(Graphics g)
{
g.drawRect(10, 10, 60, 50);
g.fillRect(100, 10, 60, 50);
g.drawRoundRect(190, 10, 60, 50, 15, 15);
g.fillRoundRect(70, 90, 140, 100, 30, 40);
}
}
Drawing Arcs:
Arcs can be drawn with drawArc( ) and fillArc( ), shown here:
void drawArc(int X, int Y, int width, int height, int startAngle, int sweepAngle)
void fillArc(int X, int Y, int width, int height, int startAngle, int sweepAngle)
The arc is bounded by the rectangle whose upper-left corner is specified by X,Y and
whose width and height are specified by width and height. The arc is drawn from
startAngle through the angular distance specified by sweepAngle. Angles are specified in
9
Notes by – Mandar Sohani
CO6: Develop web based applications.
ES05-Object Oriented Programming FE SEM II Mandar Sohani
degrees. Zero degrees is on the horizontal, at the three o’clock position. The arc is drawn
counterclockwise if sweepAngle is positive, and clockwise if sweepAngle is negative.
Therefore, to draw an arc from twelve o’clock to six o’clock, the start angle would be 90
and the sweep angle 180.
import java.awt.*;
import java.applet.*;
/*
<applet code="Arcs" width=300 height=200>
</applet>
*/
public class Arcs extends Applet
{
public void paint(Graphics g)
{
g.drawArc(10, 40, 70, 70, 0, 75);
g.fillArc(100, 40, 70, 70, 0, 75);
g.drawArc(10, 100, 70, 80, 0, 175);
g.fillArc(100, 100, 70, 90, 0, 270);
g.drawArc(200, 80, 80, 80, 0, 180);
}
}
Drawing Polygons:
It is possible to draw arbitrarily shaped figures using drawPolygon( ) and fillPolygon( ),
shown here:
void drawPolygon(int x[ ], int y[ ], int numPoints)
void fillPolygon(int x[ ], int y[ ], int numPoints)
The polygon’s endpoints are specified by the coordinate pairs contained within the x and
y arrays. The number of points defined by x and y is specified by numPoints.
import java.awt.*;
import java.applet.*;
/*
<applet code="HourGlass" width=230 height=210>
</applet>
*/
public class HourGlass extends Applet
{
public void paint(Graphics g)
{
int xpoints[] = {30, 200, 30, 200, 30};
int ypoints[] = {30, 30, 200, 200, 30};
int num = 5;
g.drawPolygon(xpoints, ypoints, num); LIBRARY
}
}
10
Notes by – Mandar Sohani
CO6: Develop web based applications.
ES05-Object Oriented Programming FE SEM II Mandar Sohani
import java.awt.*;
import java.applet.*;
/*
<applet code="ColorDemo" width=300 height=200>
</applet>
*/
public class ColorDemo extends Applet
{
public void paint(Graphics g)
{
Color c1 = new Color(255, 100, 100);
Color c2 = new Color(100, 255, 100);
Color c3 = new Color(100, 100, 255);
g.setColor(c1);
g.drawLine(0, 0, 100, 100);
g.drawLine(0, 100, 100, 0);
g.setColor(c2);
g.drawLine(40, 25, 250, 180);
g.drawLine(75, 90, 400, 400);
g.setColor(c3);
g.drawLine(20, 150, 400, 40);
g.drawLine(5, 290, 80, 19);
g.setColor(Color.red);
g.drawOval(10, 10, 50, 50);
g.fillOval(70, 90, 140, 100);
g.setColor(Color.blue);
g.drawOval(190, 10, 90, 30);
g.drawRect(10, 10, 60, 50);
g.setColor(Color.cyan);
g.fillRect(100, 10, 60, 50);
g.drawRoundRect(190, 10, 60, 50, 15, 15);
}
}
11
Notes by – Mandar Sohani
CO6: Develop web based applications.