Module 5
Module 5
Module 5
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. After an
applet arrives on the client, it has limited access to resources so that it can produce a
graphical user interface and run complex computations without introducing the risk
of viruses or breaching data integrity.
Let’s begin with the simple applet shown here:
Example 1: Simple Applet
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("A Simple Applet", 20, 20);
}
}
This applet begins with two import statements. The first imports the Abstract
Window Toolkit (AWT) classes. Applets interact with the user (either directly or
indirectly) through the AWT, not through the console-based I/O classes. The AWT
contains support for a window-based, graphical user interface. The second import
statement imports the applet package, which contains the class Applet. Every applet
that you create must be a subclass of Applet. The next line in the program declares
the class SimpleApplet. This class must be declared as public, because it will be
accessed by code that is outside the program. Inside SimpleApplet, paint( ) is
declared. This method is defined by the AWT and must be overridden by the applet.
paint( ) is called each time that the applet must redisplay its output. 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 contains the graphics context, which describes the
1
Java Programming - Module 5
graphics environment in which the applet is running. This context is used whenever
output to the applet is required.
The applet does not have a main( ) method. Unlike Java programs, applets do not
begin execution at main( ). After you entering the source code, compile the applet in
the same way that you have been compiling programs. Running an applet involves a
different process.
In fact, there are two ways in which you can run an applet:
• Executing the applet within a Java-compatible web browser.
• Using an applet viewer, such as the standard tool, appletviewer.
An applet viewer executes applet in a window. This is generally the fastest and
easiest way to test an applet. To execute an applet in a web browser, you need to
write a short HTML text file that contains a tag that loads the applet. Currently,
SunMicrosystems recommends using the APPLET tag for this purpose.
Here is the HTML file that executes SimpleApplet:
<applet code="SimpleApplet" width=200 height=60>
</applet>
The width and height statements specify the dimensions of the display area used by
the applet. After you create this file, you can execute your browser and then load this
file, which causes SimpleApplet to be executed.
More convenient method for applets is simply including a comment at the head of the
java source code file that contains an <applet> tag.
Therefore the SimpleApplet code looks like this:
Example 2: Simple Applet
import java.awt.*;
import java.applet.*;
/*<applet code=”SimpleApplet” width=200 height=60>
</applet> */
public class SimpleApplet extends Applet
{
2
Java Programming - Module 5
3
Java Programming - Module 5
// An Applet skeleton.
import java.awt.*;
import java.applet.*;
/*<applet code="AppletSkel" width=300 height=100>
</applet>*/
public class AppletSkel extends Applet
{
// Called first.
public void init()
{
// initialization
}
/* Called second, after init().
Also called whenever the applet is restarted. */
public void start()
{
// start or resume execution
}
// Called when the applet is stopped.
public void stop()
{
// suspends execution
}
/* Called when applet is terminated. This is the last method executed. */
public void destroy()
{
// perform shutdown activities
}
// Called when an applet's window must be restored.
public void paint(Graphics g)
{
// redisplay contents of window
}
}
APPLET INITIALIZATION AND TERMINATION
4
Java Programming - Module 5
It is important to understand the order in which the various methods shown in the
skeleton are called. When an applet begins, the following methods are called, in this
sequence:
1. init( )
2. start( )
3. paint( )
When an applet is terminated, the following sequence of method calls takes place:
1. stop( )
2. destroy( )
Let’s look more closely at these methods.
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 run time of your 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 your 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( )
5
Java Programming - Module 5
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. You should use stop( ) to suspend threads that
don’t need to run when the applet is not visible. You can restart them when start( ) is
called if the user returns to the page.
destroy( )
The destroy( ) method is called when the environment determines that your applet
needs to be removed completely from memory. At this point, you should free up any
resources the applet may be using. The stop( ) method is always called before
destroy( ).
APPLET LIFE CYCLE
Every Java applet inherits a set of default behaviours from the Applet class. As a
result when an applet is loaded it undergoes a series of changes in its state as shown
in the figure below.
User defined parameters can be supplied to an applet using <PARAM> tags. The
PARAM tag allows you to specify applet-specific arguments in an HTML page.
Applets access their attributes with the getParameter( ) method. It returns the value
of the specified parameter in the form of a String object.
Example 4: Simple Applet retrieving parameter
import java.applet.*;
import java.awt.*;
8
Java Programming - Module 5
One of the most important features of Java is its ability to draw graphics. We can
write Java applets that draw lines, figures of different shapes, images and text in
different fonts and styles. Every applet has its own area of the screen known as
canvas , where it creates its display. A Java applet draws graphical image inside its
space using the coordinate system as shown below.
Java coordinate system has the origin (0,0) in the upper left corner. Positive x values
are to the right, and positive y values are to the bottom. The values of coordinates x
and y are in pixels.
The Graphics Class
Java’s Graphics class includes methods for drawing many different shapes, from
simple lines to polygons to text in a variety of fonts.
Drawing methods of Graphics class
Method Description
clearRect() Erases a rectangular area of the canvas.
copyArea() Copies the rectangular area of the canvas to another area.
drawArc() Draws a hollow arc
drawLine() Draws a straight line
drawOval() Draws a hollow oval
drawPolygon() Draws a hollow polygon
9
Java Programming - Module 5
10
Java Programming - Module 5
The fillRect() method can be used to draw a solid box. This also takes four arguments
(same as drawRect() method)
Example :: g.fillRect(10,60,40,30);
Rounded rectangles (Rectangles with rounded edges) can be created using the
methods drawRoundRect() and fillRoundRect() . These methods are similar to
drawRect() and fillRect() except that they take two extra arguments representing
the width and height of the angle of corners. These extra parameters indicate how
much of corners will be rounded.
Example:: g.drawRoundRect(10,100,80,50,10,10);
g.fillRoundRect(10,100,80,50,10,10);
Circles and Ellipses
The Graphics class does not have any method for circles or ellipses. However
drawOval() method can be used to draw a circle or ellipse. Ovals are just like
rectangles with overly rounded corners. drawOval() method takes four arguments:
the first two represent the top left corner of the imaginary rectangle and the
other two represent the width and height of the oval itself. If width and height are
the same, the oval becomes a circle. Ovals coordinates are actually the coordinates of
an enclosing rectangle.
Like rectangle methods, drawOval() method draws outline of an oval, and the
fillOval() method draws a solid Oval.
Example 5: lines,rectangles, circles , ovals and rounded rectangles
import java.awt.*;
import java.applet.*;
/*<applet code="LineRect " width=500 height=200></applet>*/
public class LineRect extends Applet
{
public void paint(Graphics g)
{
g.drawLine(10,10,50,50);
g.drawRect(10,60,40,30);
11
Java Programming - Module 5
g.setColor(Color.blue);
g.fillRect(60,10,30,80);
g.setColor(Color.green);
g.drawRoundRect(10,100,80,50,10,10);
g.setColor(Color.yellow);
g.fillRoundRect(20,110,60,30,5,5);
g.setColor(Color.red);
g.drawLine(100,10,230,140);
g.drawLine(100,140,230,10);
g.drawString("Line Rectangles Demo",65,180);
g.drawOval(230,10,200,150);
g.setColor(Color.blue);
g.fillOval(245,25,100,100);
}
}
Output
Drawing Arcs
An arc is a part of an oval. The drawArc(), designed to draw arcs ,takes six
arguments. The first four are same arguments as that of drawOval() method and
last two arguments represent the starting angle of the arc and the number of
degrees(sweep angle) around the arc. In drawing arcs , Java actually formulates the
arc as an oval and then draws only a part of it as dictated by the last two arguments.
12
Java Programming - Module 5
Drawing polygons
Polygons are shapes with many sides. A polygon may be considered a set of lines
connected together. The end of first line is the beginning of the second line, and end
of the second is the beginning of the third and so on. We can draw polygon with n
sides using the drawLine() method n times in succession. We can draw polygons
more conveniently using the drawPolygon() method of Graphics class.
This method takes three arguments.
13
Java Programming - Module 5
14