Home | What is an Applet?

Chapter: Web or internet Programming : Applets

What is an Applet?

According to Sun "An applet is a small program that is intended not to be run on its own, but rather to be embedded inside another application....The Applet class provides a standard interface between applets and their environment."

What is an Applet?

 

 

According to Sun "An applet is a small program that is intended not to be run on its own, but rather to be embedded inside another application....The Applet class provides a standard interface between applets and their environment."

 

Four definitions of applet:

 

·         A small application

 

·         A secure program that runs inside a web browser

 

·         A subclass of java.applet.Applet

 

·         An instance of a subclass of java.applet.Applet

 

 

public class Applet extends Panel

 

 

java.lang.Object

 

|

 

+----java.awt.Component

 

|

 

+----java.awt.Container

 

|

 

+----java.awt.Panel

 

|

 

+----java.applet.Applet

 

 

Hello World: The Applet

 

 

import java.applet.Applet; import java.awt.Graphics;

 

public class HelloWorldApplet extends Applet {

 

public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); }}

 

The applet version of HelloWorld is a little more complicated than the HelloWorld application, and it will take a little more effort to run it as well.

 

First type in the source code and save it into file called HelloWorldApplet.java. Compile this file in the usual way. If all is well a file called HelloWorldApplet.class will be created. Now you need to create an HTML file that will include your applet. The following simple HTML file will do.

 

<HTML>

<HEAD>

 

<TITLE> Hello World </TITLE> </HEAD>

 

<BODY>

This is the applet:<P>

 

<applet code="HelloWorldApplet.class" width="150" height="50"> </applet>

 

</BODY>

</HTML>

 

Save this file as HelloWorldApplet.html in the same directory as the HelloWorldApplet.class file. When you've done that, load the HTML file into a Java enabled Web Browser..

 

The APPLET HTML Tag

 

 

Applets are embedded in web pages using the <APPLET> and </APPLET> tags. The <APPLET> tag is similar to the <IMG> tag. Like <IMG> <APPLET> references a source file that is not part of the HTML page on which it is embedded. IMG elements do this with the SRC attribute. APPLET elements do this with the CODE attribute. The CODE attribute tells the browser where to look for the compiled .class file. It is relative to the location of the source document.

 

The CODEBASE attribute is a URL that points at the directory where the .class file is. The CODE attribute is the name of the .class file itself. For instance if on the HTML page of the previous section you had written

 

<APPLET

CODE="HelloWorldApplet.class"

 

 

CODEBASE="classes" WIDTH=200 HEIGHT=200>

 

</APPLET>

 

 

then the browser would have tried to find HelloWorldApplet.class in the classes directory in the same directory as the HTML page that included the applet.

 

The HEIGHT and WIDTH attributes work exactly as they do with IMG, specifying how big a rectangle the browser should set aside for the applet. These numbers are specified in pixels and are required.

 

Spacing Preferences

 

 

The <APPLET> tag has several attributes to define how it is positioned on the page.

 

 

The ALIGN attribute defines how the applet's rectangle is placed on the page relative to other elements. Possible values include LEFT, RIGHT, TOP, TEXTTOP, MIDDLE, ABSMIDDLE, BASELINE, BOTTOM and ABSBOTTOM. This attribute is optional.

 

You can specify an HSPACE and a VSPACE in pixels to set the amount of blank space between an applet and the surrounding text. The HSPACE and VSPACE attributes are optional.

 

<APPLET

code="HelloWorldApplet.class"

CODEBASE="http://www.foo.bar.com/classes"

width=200

height=200

ALIGN=RIGHT

HSPACE=5

VSPACE=10>

</APPLET>

 

The ALIGN, HSPACE, and VSPACE attributes are identical to the attributes of the same name used by the <IMG> tag.

 

The <APPLET> has an ALT attribute. An ALT attribute is used by a browser that understands the APPLET tag but for some reason cannot play the applet. , then the browser should display the ALT text.

 

 

<applet code="HelloWorldApplet.class" CODEBASE="http://www.foo.bar.com/classes" width=200 height=200 ALIGN=RIGHT HSPACE=5 VSPACE=10

 

ALT="Hello World!"> </APPLET>

 

Naming Applets

 

 

You can give an applet a name by using the NAME attribute of the APPLET tag. This allows communication between different applets on the same Web page.

 

 

<APPLET

code="HelloWorldApplet.class"

Name=Applet1

CODEBASE="http://www.foo.bar.com/classes"

width=200

height=200

 

ALIGN=RIGHT HSPACE=5 VSPACE=10 ALT="Hello World!">

 

Hello World!<P> </APPLET>

 

JAR Archives

 

 

HTTP 1.0 uses a separate connection for each request. When you're downloading many small files, the time required to set up and tear down the connections can be a significant fraction of the total amount of time needed to load a page. It would be better if you could load all the HTML documents, images, applets, and sounds a page needed in one connection.

 

One way to do this without changing the HTTP protocol, is to pack all those different files into a single archive file, perhaps a zip archive, and just download that.

 

You can pack all the images, sounds, and .class files an applet needs into one JAR archive and load that instead of the individual files. Applet classes do not have to be loaded directly. They can also be stored in JAR archives. To do this you use the ARCHIVES attribute of the APPLET tag

 

<APPLET

CODE=HelloWorldApplet

 

WIDTH=200 HEIGHT=100

 

ARCHIVES="HelloWorld.jar">

<hr>

 

Hello World!

<hr>

</APPLET>

 

In this example, the applet class is still HelloWorldApplet. However, there is no HelloWorldApplet.class file to be downloaded. Instead the class is stored inside the archive file HelloWorld.jar.

 

The OBJECT Tag

 

 

HTML 4.0 deprecates the <APPLET> tag. Instead you are supposed to use the <OBJECT> tag. For the purposes of ewbedding applets, the <OBJECT> tag is used almost exactly like the <APPLET> tag except that the class attribute becomes the classid attribute. For example,

 

 

<OBJECT classid="MyApplet.class" CODEBASE="http://www.foo.bar.com/classes" width=200 height=200 ALIGN=RIGHT HSPACE=5 VSPACE=10>

 

</OBJECT>

 

 

 

Finding an Applet's Size

 

 

When running inside a web browser the size of an applet is set by the height and width attributes and cannot be changed by the applet. Many applets need to know their own size. After all you don't want to draw outside the lines. :-)

 

Retrieving the applet size is straightforward with the getSize() method. java.applet.Applet inherits this method from java.awt.Component. getSize() returns a java.awt.Dimension object. A Dimension object has two public int fields, height and width. Below is a simple applet that prints its own dimensions.

 

import java.applet.*; import java.awt.*;

public class SizeApplet extends Applet {

 

public void paint(Graphics g) { Dimension appletSize = this.getSize(); int appletHeight = appletSize.height; int appletWidth = appletSize.width;

 

g.drawString("This applet is " + appletHeight +

 

" pixels high by " + appletWidth + " pixels wide.", 15, appletHeight/2);

 

} }

 

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 java.applet.Applet class.

 

 

The program below demonstrates this with a generic string drawing applet. The applet parameter

 

"Message" is the string to be drawn.

 

 

import java.applet.*; import java.awt.*;

 

public class DrawStringApplet extends Applet {

 

private String defaultMessage = "Hello!";

 

public void paint(Graphics g) {

String inputFromPage = this.getParameter("Message");

 

if (inputFromPage == null) inputFromPage = defaultMessage; g.drawString(inputFromPage, 50, 25);

 

}

 

}

 

You also need an HTML file that references your applet. The following simple HTML file will

 

do:

 

 

<HTML>

<HEAD>

<TITLE> Draw String </TITLE> </HEAD>

 

<BODY>

This is the applet:<P>

 

<APPLET code="DrawStringApplet.class" width="300" height="50"> <PARAM name="Message" value="Howdy, there!">

</APPLET>

</BODY>

</HTML>

 

 

You pass getParameter() a string that names the parameter you want. This string should match the name of a <PARAM> tag in the HTML page. getParameter() returns the value of the parameter. All values are passed as strings.

 


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Web or internet Programming : Applets : What is an Applet? |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.