Applets: Applet Programs Application Programs
Applets: Applet Programs Application Programs
A main() method is not invoked on an applet, and an applet class will not define
main().
Advantage of Applet
Drawback of Applet
public class Myclass extends Applet
{
{
public static void
public void init() { }
main(String args[]) {}
public void start() { }
}
public void stop() {}
public void destroy() {}
public void paint(Graphics g) {}
}
Advantages of Applets
1. Execution of applets is easy in a Web browser and does not require any
installation or deployment procedure in real time programming (where as
servlets require).
2. Writing and displaying (just opening in a browser) graphics and animations
is easier than applications.
3. In GUI development, constructor, size of frame, window closing code etc.
are not required (but are required in applications).
Limitations of Applets:
These methods are known as life cycle methods. These methods are defined
in java.applet.Applet class except paint() method. The paint() method is
defined in java.awt.Component class, an indirect super class of Applet.
Description of Life Cycle methods
Even though, the methods are called automatically by the browser, the
programmer should know well when they are called and what he can do with
the methods. Following is the schematic representation of the methods.
Sun currently recommends that the APPLET tag be used to start an applet from
both an HTML document and from an applet viewer. An applet viewer will
execute each APPLET tag that it finds in a separate window, while web
browsers will allow many applets on a single page. So far, we have been using
only a simplified form of the APPLET tag. The syntax for a fuller form of the
APPLET tag is shown here. Bracketed items are optional.
< APPLET
[CODEBASE = codebaseURL]
CODE = appletFile
[ALT = alternateText]
[NAME = appletInstanceName]
WIDTH = pixels HEIGHT = pixels
[ALIGN = alignment]
[VSPACE = pixels] [HSPACE = pixels]
[< PARAM NAME = AttributeName VALUE = AttributeValue>]
[< PARAM NAME = AttributeName2 VALUE = AttributeValue>]
...
[HTML Displayed in the absence of Java]
</APPLET>
Let’s take a look at each part now.
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. This is distinct from the alternate HTML you provide
for browsers that don’t support 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.
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’re 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.
Other valid APPLET attributes include ARCHIVE, which lets you specify one or
more archive files, and OBJECT, which specifies a saved version of the applet.
In general, an APPLET tag should include only a CODE or an OBJECT attribute,
but not both.
Example 1:
import java.applet.*;
g.drawString("welcome",500,500);
Example 2:
import java.io.*;
import java.awt.*;
import java.applet.*;
Requesting Repainting
As a general rule, an applet writes to its window only when its update( )
or paint( ) method is called by the AWT. Whenever your applet needs to
update the information displayed in its window, it simply calls repaint( ).
The repaint( ) method is defined by the AWT. It causes the AWT run-
time system to execute a call to your applet’s update( ) method, which,
in its default implementation, calls paint( ). Thus, for another part of
your applet to output to its window, simply store the output and then
call repaint( ). Calling repaint( ) is essentially a request that your applet
be repainted sometime soon.
As just discussed, the APPLET tag in HTML allows you to pass parameters to
your applet. To retrieve a parameter, use the getParameter( ) method. It
returns the value of the specified parameter in the form of a String object.
Thus, for numeric and boolean values, you will need to convert their string
representations into their internal formats. Here is an example that
demonstrates passing parameters:
import java.applet.*;
import java.awt.*;
import java.io.*;
</applet>*/
int d=0;
g.drawString("name :"+getParameter("name"),50,50);
g.drawString("designation :"+getParameter("designation"),50,50);
g.drawString("department :"+getParameter("department"),50,50);
g.drawString("college :"+getParameter("college"),50,50);
d=Integer.parseInt(getParameter("salary"));
g.drawString("salary :"+d,50,100);
}
Graphics class
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.
Let’s take a look at several of the drawing methods.
Drawing Lines
void drawRoundRect(int top, int left, int width, int height,int xDiam, int yDiam)
void fillRoundRect(int top, int left, int width, int height,int xDiam, int yDiam)
To draw an ellipse, use drawOval( ). To fill an ellipse, use fillOval( ). These
methods are shown here:
void drawArc(int top, int left, int width, int height, int startAngle,int
sweepAngle)
void fillArc(int top, int left, int width, int height, int startAngle,int sweepAngle)
Drawing Polygons
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. There are alternative forms of these methods in which the polygon
is specified by a Polygon object.
The following applet draws an hourglass shape:
// Draw Polygon
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);
}
}
Color class
The first constructor takes three integers that specify the color as a mix
of red, green, and
blue. These values must be between 0 and 255, as in this example:
new Color(255, 100, 100); // light red
The second color constructor takes a single integer that contains the mix
of red, green, and
blue packed into an integer. The integer is organized with red in bits 16 to 23,
green in bits 8 to 15, and blue in bits 0 to 7. Here is an example of this
constructor:
The final constructor, Color(float, float, float), takes three float values
(between 0.0 and 1.0)
that specify the relative mix of red, green, and blue.
Once you have created a color, you can use it to set the foreground and/or
background color by using the setForeground( ) and setBackground( )
methods.
Here, newColor specifies the new color. The class Color defines the constants
shown here
that can be used to specify colors:
The following example sets the background color to green and the text color to
red:
setBackground(Color.green);
setForeground(Color.red);
A good place to set the foreground and background colors is in the init( )
method. Of
course, you can change these colors as often as necessary during the execution
of your applet.
You can obtain the current settings for the background and foreground colors
by calling:
getBackground( ) and getForeground( ), respectively. They are also defined by
Component
and are shown here:
Color getBackground( )
Color getForeground( )
Here is a very simple applet that sets the background color to cyan, the
foreground color
to red, and displays a message that illustrates the order in which the init( ),
start( ), and paint( )
methods are called when an applet starts up:
The Color class defines several methods that help manipulate colors.
The Brightness values also range from 0.0 to 1.0, where 1 is bright white
and 0
is black.
Color supplies two methods that let you convert between RGB and HSB.
They are shown here:
You can obtain the red, green, and blue components of a color independently
using getRed( ),
getGreen( ), and getBlue( ), shown here:
int getRed( )
int getGreen( )
int getBlue( )
Each of these methods returns the RGB color component found in the invoking
Color object
in the lower 8 bits of an integer.
getRGB( )
To obtain a packed, RGB representation of a color, use getRGB( ), shown here:
int getRGB( )
You can obtain the current color by calling getColor( ), shown here: getColor( )
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);
}
}