Java Applets

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 an arbitrary multimedia user interface and run complex computations without introducing the risk of viruses or breaching data integrity.
applets – Java program that runs within a Java-enabled browser, invoked through a “applet” reference on a web page, dynamically downloaded to the client computer
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString("A Simple Applet", 20, 20);
}
}
There are two ways to run an applet:
1. Executing the applet within a Java-compatible Web browser, such as NetscapeNavigator.
2. Using an applet viewer, such as the standard JDK tool, Appletviewer.
3. An appletviewer executes your 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" width=200 height=60>
</applet>

Differences between applets and applications:
1. Applets do not have main() method in them. On loading of applets some methods of applet class get called automatically.
2. Applets can not run independently. They can be either executed by embedding them into the web page or using the appletviewer they can be executed,
3. Applets can not be read from a file. Similarly applets can not write to files in the local system.
4. Applet can not execute any program on local computer.
5. Applet can not communicate with other servers on the network.
6. Applets can not use the library files of other languages.
These are the limitations and restrictions that are put on the applets for creating a secured system.



Class Hierarchy of Applet Class:

The AWT allows us to use various graphical components. When we start writing any applet program we essentially import two packages namely – java.awt and java.applet.

The java .applet package contains a class Applet which uses various interfaces such as AppletContextAppletStub and AudioClip. The applet class is an extension of panel class belonging to java.awt package.

To create an user friendly graphical interface we need to place various components on GUI window. There is a Component class from java.awt package which derives several classes for components. These classes include Checknox, choice, List, buttons and so on. The Component class in java.awt is an abstract class.




Applets life cycle includes the following methods
init( )
start( )
paint( )
stop( )
destroy( )

When an applet begins, the AWT calls the following methods, in this sequence:
init( )
start( )
paint( )
When an applet is terminated, the following sequence of method calls takes place:
stop( )
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.

Example:
 public void init()
{
//initialization of variables
}
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( ).

Example:
 public void start()
{
}
paint( ): The paint( ) method is called each time applet's output must be redrawn.  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.

Example:
 public void paint(Graphics g)
{
...
}

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. 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.

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

Example:
 public void destroy()
{
...
}
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

Creating applets

Applets are created by extending the Applet class.
import java.awt.*;
import java.applet.*;
/*<applet code="AppletSkel" width=300
 height=100></applet> */
public class AppletSkel extends Applet {
public void init() {
// initialization
}
public void start() {
// start or resume execution
}
public void stop() {
// suspends execution
}
public void destroy() {
// perform shutdown activities
}
public void paint(Graphics g) {
// redisplay contents of window
}
}


Details about Applet Tag

The <Applet> tag can be specified with the help of various attributes. These attributes help to integrate the applet into overall design of web page.

Attribute
Description
CODE=appletfilename
The specified applet can be loaded in the web page. This attribute must be specified in order to embed the applet in the HTML file
WIDTH=pixels
HEIGHT=pixels
This attribute specifies the width and height of the applet.
ALIGN=alignment
This attribute is for specifying the alignment. Various alignments are TOP,BOTTOM, LEFT, RIGHT, MIDDLE, ABSMIDDLE, ABSBOTTOM, TEXTTOP and BASELINE. This is an optional attribute.
CODEBASE=codebase_url
It specifies the name of the directory in which the applet is stored. This attribute is required when the attribute is not there in the current working directory. This is an optional attribute.
HSPACE=pixels
When ALIGN is either LEFT or RIGHT this attribute is used.
VSPACE=pixels
When ALIGN is either TOP or BOTTOM this attribute is used.
ALT=alternate text
The non java browser can display the alternate text in place of applet. This is an optional attribute.

Passing parameters to Applets

Parameters are passed to applets in NAME=VALUE pairs in <PARAM> tags between the opening and closing APPLET tags.
Inside the applet, you read the values passed through the PARAM tags with the getParameter() method of the Applet class.

Example:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="AppletParam" ALT="sorry for the interruption" width=300 height=200>
<PARAM name="Message" value="CSE-II of VITAE">
</applet>
*/
public class AppletParam extends Applet
{
public void paint(Graphics g)
{
String str=this.getParameter("Message");
if(str==null)
 str="FRIENDS";
else
 str="Welcome "+str;
g.drawString(str,50,20);
}
}
Colors in Applet:

Method used to set the background color of an Applet.
 void setBackground(Color.colorname);

Method used to set the foreground color of an Applet.
void setForeground(Color.colorname);

Example:
import java.applet.*;
import java.awt.*;
/*
<applet code="ColorDemo" width=300 height=100>
</applet>
*/
public class ColorDemo extends Applet
{
public void paint(Graphics g)
{
setBackground(Color.cyan);
setForeground(Color.red);
g.drawString("It is a colorful applet",50,30);
Color newColor=new Color(255,255,0);
g.setColor(newColor);
g.drawString("It is a colorful applet",50,70);
}
}



أحدث أقدم