The Applet Class

The java.applet package is the smallest package in the Java API. The Applet class is contained i

The java.applet package is the smallest package in the Java API. The Applet class is contained in the java.applet package. Applet contains several methods that gives us detailed control over the execution of the applet. In addition, java.applet also define three interfaces: AppletContext, AppletStub, and AudioClip.

All applets are subclasses of Applet. Thius, all applets must import java.applet. applets must also import java.awt. 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, they are executed by either a Web Browser or an applet viewer.

The Applet class contains a single default parameterless constructor, which is generally not used. Applets are constructed by the runtime environment when they are loaded and do not have to be explicitly constructed.

Applet provides all necessary support for applet execution, such as starting and stopping. It also provides methods that load and display images, and methods and methods that load and play audio clips.

 

In order to run a Java applet, it is first necessary to have a web page that references the applet. The <Applet> tag supplies the name of the applet to be loaded and tells the browser how much space the applet requires. Applet tag takes zero or more parameters.

Syntax

<APPLET

CODE=”name of the class file that extends java.applet.Applet”

CODEBASE=”path of the class file” HEIGHT=”maximum height of the applet, in pixels”

WIDTH=”maximum width of the applet, in pixels”

VSPACE=”vertical space between the applet and the rest of the HTML”

HSPACE=”horizontal space between the applet and the rest of the HTML”

ALIGN=”alignment of the applet with respect to the rest of the web page”

ALT=”alternate text to be displayed if the browser does not support applets”

>

<PARAM NAME=”parameter_name” value=”value_of_parameter”>

</APPLET>

The most commonly used attributes of the Applet tag are CODE, HEIGHT, WIDTH, CODEBASE and ALT. Also you can send parameter to the applet using the PARAM tag. The PARAM tag must be written between <APPLET> and </APPLET> .

CODE: this is required attribute that gives the name of the file containing your applet’s compiled .class file.

CODEBASE: this is an optional attribute, that specifies the base URL of the applet code.

HEIGHT AND WIDTH : these two are required attributes that gives the size in pixels of the applet display area.

VSPACE AND HSPACE: These attributes are optional. VSPACE specifies the space, in pixels, above and bellow the applet. HSPACE specifies the space, in pixels, on each side of the applet.

ALIGN: this is an optional attribute that specifies the alignment of the applet. The possible values are LEFT, RIGHT, TOP, BOTTOM, MIDDLE, BASELINE, TEXTTOP, ABSMIDDLE and ABSBOTTOM.

ALT: this is also an optional attribute used to specify a short text message that should be displayed if the browser can not run the Java Applet.

PARAM NAME AND VALUE: the PARAM tag allows to specify applet specific arguments in an HTML page.

Example 1 : Simple Applet displaying HELLO WORLD

SimpleJavaApplet.java

import java.awt.Graphics;

//This imports our Graphics class , which is used for drawing lines , squares, circles, text.

 public class SimpleJavaApplet extends java.applet.Applet {

//This means that our custom HelloWorld class extends the Applet class. That is , it is a subclass of Applet. Therefore we have access to all the methods of Applet, and we can extend our custom class to do further things.

            public void paint(Graphics g) {

//This method is our drawing method. This method draws anything that is in our applet to the screen. Note that we have passed an instance(g) of the Class Graphics to it.                   

      g.drawString(“HELLO WORLD”, 5, 25);

//drawString is a method of the Graphics class. We have an instance of the Graphics class – g. drawString takes a String and an x and a y coordinate.

            }

}

simpleapplet.html

<html>

<head>

<body>

 <APPLET CODE=”SimpleJavaApplet.class” HEIGHT=250 WIDTH=250 >

 </APPLET>

 </body>

</html>

Both this file needs to stay in the same directory.

The HTML code tells the browser to load the compiled java applet SimpleJavaApplet.class that is in the same directory as this HTML file. It specifies the display area for the applet output as 250 pixels width and 250 pixels height.

To run an applet, we require one of the following tools:

a)            Java Applet Viewer

b)            Java enabled Web Brower (such as IE or Firefox )

Before running the program you need to compile the program. The next section demonstrate how to compile and run an applet in a graphical way. The example shows here are keep in a directory under c:\html. 

a)    When the browser encounters an <Applet> tag, it reserves a display area of the specified width and height for the class in the browser window, loads the bytecodes for the specified Applet subclass, creates an instance of the subclass and then calls for the instances init and start method.

b)   

We can embed applets into Web pages in two ways.

One we can write our own applets and embed them into web pages. An applet developed locally and stored in a local system is known as a local applet.

Second, we can download an applet from a remote computer system and then embed it into a web page.  A remote applet is that which is developed by someone else and stored on a remote computer (web server) connected to the Internet. In order to locate and load a remote applet, we must know the applets address on the web. This address is known as the URL and must be specified in the applet’s HTML document as the value of the CODEBASE attribute.

Drawing Shapes Example:

In this program we will see how to draw the different types of shapes like line, circle and rectangle. There are different types of methods for the Graphics class of the java.awt.*; package have been used to draw the appropriate shape.

Here is the java code of the program :

CircleLine.java:

import java.applet.*;

import java.awt.*;

public class  CircleLine extends Applet{

int x=300,y=100,r=50;

public void paint(Graphics g){

g.drawLine(3,300,200,10);      //  used to draw the line in the applet.

g.drawString(“Line”,100,100); // draws the given string as the parameter

g.drawOval(x-r,y-r,100,100); //  draws the circle

g.drawString(“Circle”,275,100); // draws the given string as parameter

g.drawRect(400,50,200,100); // draws the rectangle

g.drawString(“Rectangel”,450,100); // draws the given string as parameter

  }

}

Here is the HTML code of the program:

CircleLine.html:

<HTML>

<HEAD>

</HEAD>

<BODY>

<div align=”center”>

<APPLET CODE=”CircleLine.class” WIDTH=”800″ HEIGHT=”500″></APPLET>

</BODY>

</HTML>

Leave a Reply

Your email address will not be published. Required fields are marked *

5  ×    =  45