KEMBAR78
Mechanism. Output To Your Applet's Window Is Not Performed by | PDF | Computer Programming | Software
0% found this document useful (0 votes)
78 views5 pages

Mechanism. Output To Your Applet's Window Is Not Performed by

Applet Basics There are two types of applets - those based on the Applet class using AWT and those based on the JApplet class using Swing. Applets run within a web browser or applet viewer and do not have a main method. Execution is started by init(), start(), stop(), and destroy() methods. Output is done through AWT/Swing methods rather than System.out. Applets are specified in HTML and viewed using an applet viewer. The Applet class provides window support and methods for images/audio.

Uploaded by

mail.sushilk8403
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)
78 views5 pages

Mechanism. Output To Your Applet's Window Is Not Performed by

Applet Basics There are two types of applets - those based on the Applet class using AWT and those based on the JApplet class using Swing. Applets run within a web browser or applet viewer and do not have a main method. Execution is started by init(), start(), stop(), and destroy() methods. Output is done through AWT/Swing methods rather than System.out. Applets are specified in HTML and viewed using an applet viewer. The Applet class provides window support and methods for images/audio.

Uploaded by

mail.sushilk8403
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/ 5

Applet Basics

All applets are subclasses (either directly or indirectly) of Applet. Applets are not stand-alone
programs. Instead, they run within either a web browser or an applet viewer.

There are two varieties of applets.

1. The first are those based directly on the Applet class. These applets use the Abstract
Window Toolkit (AWT) to provide the graphic user interface (or use no GUI at all).
2. The second type of applets are those based on the Swing class JApplet. Swing applets
use the Swing classes to provide the GUI. Swing offers a richer and often easier-to-use
user interface than does the AWT. Thus, Swing-based applets are now the most popular.

Because JApplet inherits Applet, all the features of Applet are also available in JApplet.
Execution of an applet does not begin at main( ). Actually, few applets even have main( )
methods. Instead, execution of an applet is started and controlled with an entirely different
mechanism. Output to your applet’s window is not performed by System.out.println( ).
Rather, in non-Swing applets, output is handled with various AWT methods, such as
drawString( ), which outputs a string to a specified X,Y location. Input is also handled
differently than in a console application.

To use an applet, it is specified in an HTMLfile. One way to do this is by using the APPLET
tag. The applet will be executed by a Java-enabled web browser when it encounters the
APPLET tag within the HTMLfile. To view and test an applet more conveniently, simply
include a comment at the head of your Java source code file that contains the APPLET tag.
This way, your code is documented with the necessary HTML statements needed by your
applet, and you can test the compiled applet by starting the applet viewer with your Java source
code file specified as the target. Here is an example of such a comment:

/* <applet code="MyApplet" width=200 height=60> </applet> */

This comment contains an APPLET tag that will run an applet called MyApplet in a window
that is 200 pixels wide and 60 pixels high.

The Applet Class


The Applet class defines the methods shown in Table 21-1. 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. These classes provide
support for Java’s window-based, graphical interface. Thus, Applet provides all of the
necessary support for window-based activities.

An Applet Skeleton
Mostly applets override a set of methods that provides the basic mechanism by which the
browser or applet viewer interfaces to the applet and controls its execution. Four of these
methods, init( ), start( ), stop( ), and destroy( ), apply to all applets and are defined by Applet.
Default implementations for all of these methods are provided. Applets do not need to override
those methods they do not use. However, simple applets will not need to define all of them.
AWT-based applets also override the paint( ) method, which is defined by the AWT
Component class. This method is called when the applet’s output must be redisplayed. These
five methods are shown here:
// 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
}
}

Although this skeleton does not do anything, it can be compiled and run. When run, it generates
the following window when viewed with an applet viewer:

Fig.1: Sample Applet

Applet Initialization and Termination


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( )

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( ):
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( ).

Overriding update( ):
In some situations, your applet may need to override another method defined by the AWT,
called update( ). This method is called when your applet has requested that a portion of its
window be redrawn. The default version of update( ) simply calls paint( ). However, you can
override the update( ) method so that it performs more subtle repainting.

Simple Applet Display Methods


Applets are displayed in a window, and AWT-based applets use the AWT to perform input and
output. to output a string to an applet, use drawString( ), which is a member of the Graphics
class. Typically, it is called from within either update( ) or paint( ). It has the following general
form:
void drawString(String message, int x, int y)

Here, message is the string to be output beginning at x,y. In a Java window, the upper-left
corner is location 0, 0. The drawstring ( ) method will not recognize newline characters. If
you want to start a line of text on another line, you must do so manually, specifying the precise
X, Y location where you want the line to begin.

To set the background color of an applet’s window, use setBackground( ). To set the
foreground color (the color in which text is shown, for example), use setForeground( ). These
methods are defined by Component, and they have the following general forms:

void setBackground(Color newColor)


void setForeground(Color newColor)

The following example sets the background color to green and the text color to red:

setBackground(Color.green);
setForeground(Color.red);

Agood 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( )

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:

import java.applet.*;
import java.awt.*;
/*<applet code="myapp" width=300 height=200></applet>*/
public class myapp extends Applet
{
String msg;
public void init()
{
setBackground(Color.darkGray);
setForeground(Color.red);
msg="init function";
}
public void start()
{
msg+="start fucntion";
}
public void paint (Graphics g)
{
msg+="paint function";
g.drawString(msg,60,60);
showStatus("running 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. This raises an interesting question: How can the applet itself cause its
window to be updated when its information changes? For example, if an applet is displaying a
moving banner, what mechanism does the applet use to update the window each time this
banner scrolls? Remember, one of the fundamental architectural constraints imposed on an
applet is that it must quickly return control to the run-time system. It cannot create a loop inside
paint( ) that repeatedly scrolls the banner, for example. This would prevent control from
passing back to the AWT. Given this constraint, it may seem that output to your applet’s
window will be difficult at best. Fortunately, this is not the case. Whenever your applet needs
to update the information displayed in its window, it simply calls repaint( ).

You might also like