Professional Documents
Culture Documents
Applets
Applets
Applet Basics
Applets are small java programs those are used for internet computing. Those are
accessed on an Internet server, transported over the Internet, automatically installed, and
run as part of a Web document.
All applets are subclasses of Applet. Thus, all applets must import java.applet
package. Applets must also import java.awt. AWT stands for the Abstract Window
Toolkit. Since all applets run in a window, it is necessary to include support for that
window.
Applets are not executed by the console-based Java run-time interpreter. Rather,
They are executed by either
1) A Java-compatible Web browser, such as Internet Explorer, Netscape Navigator. (or)
2) The standard JDK tool, appletviewer. An appletviewer executes the 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 the appropriate APPLET tag.
<applet code="SimpleApplet.class" width=200 height=60>
</applet>
To view and test an applet more conveniently, simply include a comment at the
head or end of your Java source code file that contains the APPLET tag and you can test
the compiled applet by starting the appletviewer with your Java source code file specified
as the target. Here is an example of such a comment:
/*
<applet code=" SimpleApplet.class " width=200 height=60>
</applet>
*/
Types of applets
Applets are two types
1.Simple applets
2.JApplets
Simple applets can be created by extending Applet class
JApplets can be created by extending JApplet class of javax.swing.JApplet
package
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( ).
public void start( ) { }
paint( ): The paint( ) method is called each time applet's output must be redrawn. For
example, when 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. This parameter will contain the graphics context. This context is
used whenever output to the applet is required.
public void paint(Graphics g) { }
stop( ): The stop( ) method is called when a web browser leaves the HTML
document containing the applet, i.e., when it goes to another page. When stop( ) is
called, the applet is probably running. Applet uses stop( ) to suspend threads that
don't need to run when the applet is not visible. To restart start( ) is called if the user
returns to the page.
public void stop( ) { }
destroy( ): The destroy( ) method is called when the environment determines that
your applet needs to be removed completely from memory. The stop( ) method is
always called before destroy( ).
public void destroy( ) { }
Java can be used to create two types of programs: applications and applets.
applications applets
An application is a program that runs on An applet is an application designed to
your computer, under the operating system be transmitted over the Internet and
of that Computer. executed by a Java-compatible Web
browser.
For example, this sets the background color to green and the text color to red:
setBackground(Color.green);
setForeground(Color.red);
THE JAVAA good place to set the foreground and background colors is in the init( )
method. The default foreground color is black. The default background color is light
gray.
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( )
In addition to displaying information in its window, an applet can also output a
message to the status window of the browser or applet viewer on which it is running. To
do so, call showStatus( ) with the string that you want displayed. Its general form is
void showStatus( String msg)
The following applet demonstrates setForeground( ),setBackground( )& showStatus( )
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);
setForeground(Color.red);
}
// Display msg in applet window.
public void paint(Graphics g)
{
g.drawString("This is in the applet window.", 10, 20);
showStatus("This is shown in the status window.");
You can obtain the current color by calling getColor( ), shown here:
Color getColor( )
Working with Fonts
Fonts are encapsulated by the Font class.
Creating and Selecting a Font
To select a new font, you must first construct a Font object that describes that font. One
Font constructor has this general form:
Font(String fontName, int fontStyle, int pointSize)
Here, fontName specifies the name of the desired font. All Java environments will
support the following font names: Dialog, DialogInput, Sans Serif, Serif, Monospaced,
and Symbol.
The style of the font is specified by fontStyle. It may consist of one or more of these
three constants: Font.PLAIN, Font.BOLD, and Font.ITALIC.
The size, in points, of the font is specified by pointSize.
To use a font that you have created, you must select it using setFont( ), which is defined
by Component. It has this general form:
void setFont(Font fontObj)
Here, fontObj is the object that contains the desired font.