Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org

Related items

Steps to Java Internationalization (i18n)

Java Applets in Education

Java #6 I Wanna hold your hand - longer

Mouse Event Handling in Java 1.0

Java #5 I Wanna hold your hand

Java Applets #4

Java Applets #3

Java Applets #2

Java Applets #1

You are here: irt.org | Articles | Java | Java Applets #1 [ previous next ]

Published on: Friday 6th March 1998 By: Tarique Sani

Introduction

Starting this week I will be presenting a series of articles on JAVA and how to use it to enhance your pages, for material I am relying heavily on my own experiences in page designing and how I graduated from plain HTML to JavaScript to Java. Hence my dig at not so poor Bill!

Those of you who are impatient or in a hurry to plug in a zany navigational applet on to your page, without wanting to learn much can download one now & those of you want to be sure that I am not wasting your time can see a working example

The trustworthy and the trusting please read on....

A Simple Applet tag

The very basic pre-requisite to having an applet in your page is to have an <APPLET> tag - This is what I will discuss, but first an example:

<APPLET
CODE="tarique.class" WIDTH=200 HEIGHT=40>
</APPLET>

Honest! this is a perfectly valid <APPLET> declaration, but as you might have guessed good things are not that simple (always!?)

Complete Applet Syntax

Here is the complete syntax of the applet tag:

<APPLET
    [CODEBASE=codebaseURL]
    CODE=appletFile
    [ALT=aternateText]
    [NAME=appletInstanceName]
    WIDTH=pixels HEIGHT=pixels
    [ALIGN=alignment]
    [VSPACE=pixels] [HSPACE=pixels]
>

[< PARAM NAME=attributeName VALUE=attributeValue >]
[< PARAM NAME=attributeName2 VALUE=attributeValue2 >]

.. .. ..

</APPLET>

Don't be scared - Its not as complex as it seems - Lets take one keyword at a time.

CODEBASE=codebaseURL
CODEBASE is an optional attribute that specifies the base URL of the applet code, which is the directory that will be searched for the applets executable class file (specified with the CODE tag). The HTML document's URL directory is used as the CODEBASE if this attribute is not specified. The CODEBASE does not have to be on the host that the HTML document was read from, i.e. you can use applets from my site for your pages without keeping the class file with you!

CODE=appletFile
CODE is a required attribute that gives the name of the file that contains your applet's class file. This file is relative to the code URL.

ALT=aternateText
ALT is an optional attribute which specifies a short text message that should be displayed if the browser understands the APPLET tag but can't currently run java applets.

NAME=appletInstanceName
NAME is an optional attribute used to specify a name for the applet instance. Applets must be named for other applets on the same page to find them by name and communicate.

WIDTH=pixels HEIGHT=pixels
WIDTH and HEIGHT are required attributes that give the initial size (in pixels) of the applet display area.

ALIGN=alignment
ALIGN is an optional attribute that specifies the alignment of the applet. This attribute is treated the same as the HTML IMG tag.

VSPACE=pixels HSPACE=pixels
These are optional attributes specifying the space in pixels above and below the applet. It is again the same a IMG VSPACE & HSPACE attributes.

PARAM NAME=attributeName VALUE=value
The PARAM tag is how you pass applet specific arguments to Java from HTML.

Sample ktn Applet

Lets see the <APPLET> tag for the ktn applet demonstrated above (If you trusted me - please have a look now! )

<APPLET CODE="ktn" WIDTH=450 HEIGHT=100>
    <PARAM NAME=item0   VALUE="Welcome+main.html+Welcome from Dr. Tarique Sani+splash">
    <PARAM NAME=item1   VALUE="<applet> tag+tag.html+See the Applet tag for this applet+splash">
    <PARAM NAME=item2   VALUE="Source Code+source.html+Obviously the source code for ktn.java+splash">
    <PARAM NAME=item3   VALUE="Download+download.html+Download Applet+splash">
    <PARAM NAME=welcome VALUE=" Dr Tarique Sani welcomes you, please wait ...">
    <PARAM NAME=delay   VALUE=2>

</APPLET>

OK! that was real easy stuff. When I had first designed and pasted this applet on to a friends site, before I could pat myself on the back - the phone rang! It was my friend he angrily retorted "What a lousy site, it has just one page", I said "Click on the applet", "Applet what applet? - There's just a blank area!"

Perplexed, I tested and retested the applet code before realising that the browser on my friends machine had JAVA turned off - hence he could not see the applet nor could he navigate!

Browsers without Java Support

What does one do in such a situation? Of Course - one can be elitist and say "JAVA compatible browser required", or, reconsider coding as follows.

<APPLET CODE="ktn" WIDTH=450 HEIGHT=100>
    <PARAM NAME=item0   VALUE="Welcome+main.html+Welcome from Dr. Tarique Sani+splash">
    <PARAM NAME=item1   VALUE="<applet> tag+tag.html+See the Applet tag for this applet+splash">
    <PARAM NAME=item2   VALUE="Source Code+source.html+Obviously the source code for ktn.java+splash">
    <PARAM NAME=item3   VALUE="Download+download.html+Download Applet+splash">
    <PARAM NAME=welcome VALUE=" Dr Tarique Sani welcomes you, please wait ...">
    <PARAM NAME=delay   VALUE=2>

<TABLE BORDER="0" CELLSPACING="4">
    <TR>
    <TD COLSPAN="3" BGCOLOR="#C00000"><FONT COLOR="#FFFFFF" SIZE="3" FACE="helvetica"><B>Kalptaru Net</B> - <I>JAVA applet at a glance... </I></FONT></TD>
    </TR>

    <TR>
    <TD BGCOLOR="#008000"><A HREF="main.html" TARGET="splash"><FONT COLOR="#FFFFFF">Welcome</FONT></A></TD>
    <TD BGCOLOR="#007000"><A HREF="tag.html" TARGET="splash"><FONT COLOR="#FFFFFF">Applet Tag</FONT></A></TD>
    <TD BGCOLOR="#005000"><A HREF="source.html" TARGET="splash"><FONT COLOR="#FFFFFF">Source Code</FONT></A></TD>
    </TR>

    <TR>
    <TD BGCOLOR="#007000"><FONT COLOR="#FFFFFF">Some</FONT></TD>
    <TD BGCOLOR="#006000"><FONT COLOR="#FFFFFF">More</FONT></TD>
    <TD BGCOLOR="#004000"><FONT COLOR="#FFFFFF">Options</FONT></TD>
    </TR>
</TABLE>

</APPLET>

The browsers that don't support Java will see it as:

Kalptaru Net - JAVA applet at a glance...
Welcome Applet Tag Source Code
Some More Options

As the HTML'ers amongst you must have figured out - you just stick the HTML code before the </APPLET> tag, remembering to keep the HTML code such that it fits in the WIDTH and HEIGHT of the applet, so that it does not upset the layout of your page. I personally prefer to use a table with one link in each cell.

Java Resources

That's almost all there is to the <applet> tag.

You can see lots of nifty applets at http://www.javasoft.com/java.sun.com/applets/applets.html

There's a fabulous Java programming tutorial at http://sunsite.unc.edu/javafaq/javatutorial.html but be warned its a huge download (it was 4Mb last I saw it!)

The faithful should be patient until the next Java Applets article, when I will start the dissection of an applet.

Related items

Steps to Java Internationalization (i18n)

Java Applets in Education

Java #6 I Wanna hold your hand - longer

Mouse Event Handling in Java 1.0

Java #5 I Wanna hold your hand

Java Applets #4

Java Applets #3

Java Applets #2

©2018 Martin Webb