Modern Programming Tools and Techniques-I: Lovely Professional University, Punjab
Modern Programming Tools and Techniques-I: Lovely Professional University, Punjab
Modern Programming Tools and Techniques-I: Lovely Professional University, Punjab
Introduction
A Java applet is a special kind of Java program that a javaenabled browser can download from the internet and run.
An applet is typically embedded inside a web page and runs in
the context of a browser.
An applet must be a subclass of the java.applet.Applet class.
The Applet class provides the standard interface between the
applet and the browser environment.
Types of Applets
There are two types of applets.
The first are based directly on the Applet class and use the AWT
to provide the graphic user interface.
These applets has been available since Java was first created.
Applet Class
Applet provides all necessary support for applet execution, such
as starting and stopping.
It also provides methods that load and display images, and
methods that load and play audio clips.
Applet extends the AWT class Panel. In turn, Panel extends
Container, which extends Component.
Steps to follow
Points to remember
An Applet Skeleton
An Applet Skeleton
init( )
The init( ) method is the first method to be called.
This is where we 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.
init( )is called oncethe first time an applet is loaded whereas
start( ) is called each time an applets HTML document is
displayed on screen.
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 applets 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.
The paint( ) method has one parameter of type Graphics, which
describes the graphics environment in which the applet is running.
stop( )
The stop( ) method is called when a web browser leaves the HTML
document containing the applet(e.g. when it goes to another page).
When stop( )is called, the applet may be running. We 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 the applet needs to be removed completely from
memory.
It frees up any resources the applet may be using.
The stop( ) method is always called before destroy( ).
repaint()
Whenever an 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 applets
update( ) method, which, in its default implementation, calls paint( ).
void repaint( )
void repaint(int left, int top, int width, int height)
void repaint(long maxDelay)
void repaint(long maxDelay, int x, int y, int width, int height)
Example
import java.awt.*;
import java.applet.*;
/* <applet code="Banner" width=300 height=50> </applet> */
public class Banner extends Applet implements Runnable
{
String msg = " This is a message ";
Thread t = null;
int state;
boolean stopFlag;
// Set colors and initialize thread.
public void init()
{
setBackground(Color.cyan);
setForeground(Color.red);
}
// Start thread
public void start()
{ t = new Thread(this);
stopFlag = false;
t.start(); }
public void run()
{
char ch;
// Display banner
for( ; ; ) {
try {
repaint();
Thread.sleep(450);
ch = msg.charAt(0);
msg = msg.substring(1, msg.length());
msg += ch;
if(stopFlag) break;
}
catch(InterruptedException e) {}
}
}
Example
import java.awt.*;
import java.applet.*;
/* <applet code="StatusWindow" width=300 height=50> </applet> */
public class StatusWindow extends Applet
{
public void init()
{
setBackground(Color.cyan);
}
public void paint(Graphics g)
{
g.drawString(This is applet message", 10, 20);
showStatus("This applet is running on Appletviewer.");
}
}
CODE: CODE is a required attribute that gives the name of the file
containing your applets 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 recognizes the APPLET
tag but cant currently run Java applets. This is distinct from the alternate
HTML you provide for browsers that dont support applets.
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 with these possible values:
LEFT, RIGHT, TOP, BOTTOM, MIDDLE etc.
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.
Theyre treated the same as the IMG tags VSPACE and HSPACE attributes.
PARAM NAME and VALUE The PARAM tag allows you to specify appletspecific arguments in an HTML page. Applets access their attributes with the
getParameter( ) method.
Thus, for numeric and boolean values, you will need to convert their string
representations into their internal formats.
Graphics
Drawing Text
To Draw texts on the graphics screen we use drawString()
method, shown here:
drawString(String str, int xBaselineLeft, int yBaselineLeft);
Drawing Lines
Lines are drawn by means of the drawLine( )method, shown
here:
void drawLine(int startX, int startY, int endX, int endY)
drawLine( )displays a line in the current drawing color that
begins atstartX,startYand ends at endX,endY.
Drawing Rectangles
Drawing Polygons
Color
The java.awt package defines a class named
Color.
Color.BLACK
There are 13 predefined
colors:
Color.PINK
Color.GREEN
Color.DARK_GRAY Color.RED
Color.CYAN
Color.GRAY
Color.ORANGE
Color.BLUE
Color.LIGHT_GRAY Color.YELLOW
Color.WHITE
Color.MAGENTA
You can also create your own colors, one of the color
constructors
forms is shown here:
Color(int red, int green, int blue)
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:
37
(50, 0)
(0, 20)
(50, 20)
(w-1, h-1)
39
To execute an applet
appletviewer HelloWorld.html
42