Java Applets Tutorial
Perk Up Your Web Page with Java!
By
David
Sosnowski
INTRODUCTION
Setting up JAVA applets, though perhaps daunting the first time you do it,
is really quite simple. Today we're going to walk you through the basics;
and believe it or not, you can get all this sorted out in under ten minutes.
Let's just dive in and do it!
JAVA applets use only two types of HTML tags -- both easy to deal with.
The <APPLET> tag basically just tells the browser what applet.class
file to use, and how wide and high the applet should be.
There are additional (optional) attributes you can set up, too; but in simplest
use, that's all there is to this tag, and usually all you will need.
The <PARAM> tag is likewise simple -- it NAMES a parameter the JAVA
applet needs to run, and provides a VALUE for that parameter.
Though a given applet may have anywhere from no PARAM tags to dozens, still,
every PARAM tag takes the very same simple form: just a NAME, and a VALUE.
Let's have a look at those two tags in greater detail. And please, remember
the www.CodeBrain.com maxim: "This is simple -- don't make it complicated!"
THE <APPLET> TAG
Here's the framework of a simple HTML tag set for putting an applet into
your page:
<APPLET CODE="filename.class" WIDTH="400" HEIGHT="200">
.
.
(parameters go here - more about them presently)
.
.
</APPLET>
The CODE="filename.class" contains the name of the applet's class file. The
class file is a small executable which does the real work of the applet.
For newcomers to JAVA, there are two important things to remember about the
class file: Put the class file in the same place on your server as the HTML
page calling it; and make certain you send the class file up in binary format
-- never ASCII!
Forgetting to send the class file up or sending it up in
the wrong format is responsible for about 90% of all
problems encountered while setting up applets.
Next, let's look at how to set the applet size on the page. WIDTH="400" and
HEIGHT="200" would cause the applet to appear 400 pixels wide and 200 pixels
high on your page. If you want the applet to be a different size, change
these values, just like you would for an image.
Following the <APPLET> tag you will then insert the <PARAM> tags,
and -- don't forget! -- close off this tag set with a mating
</APPLET> tag.
THE <PARAM> TAG
As we said, the <PARAM> tags go between the <APPLET> and
</APPLET> tags, as in this sample code:
<APPLET CODE="filename.class" WIDTH="400" HEIGHT="200">
<PARAM NAME="SPEED" VALUE="100">
<PARAM NAME="IMAGE1" VALUE="thisimage.gif">
<PARAM NAME="IMAGE2" VALUE="thatimage.jpg">
</APPLET>
The very first thing you will notice is that <PARAM> tags absolutely
do not, ever, have a mating end tag. <PARAM> tags are among the few
HTML tags that do not.
As for what they do, parameter tags tell the applet how it is to behave and
what resources it will use (for instance, in this example, how fast the applet
will run, and what image files it will use).
A key point for newcomers to remember is that, unlike HTML tags and JavaScript
methods or properties, a parameter's NAME is absolutely not standard. The
person who builds the applet decides what the parameter names will be, so
study the applet's documentation carefully.
However, the syntax and use of <PARAM> tags is regular, and very simple.
The NAME="whatever" specifies the parameter to be set, and its corresponding
VALUE="whatever" says what its value is to be.
In the example above, the SPEED parameter is being set to a value of 100
(probably in milliseconds, but you would check the applet documentation to
find out). Likewise, the IMAGE1 and IMAGE2 parameters would tell this applet
to use "thisimage.gif" and "thatimage.jpg" respectively for its image resources.
And that's it for the parameters! No matter how many parameters, they'll
all follow the same simple pattern.
PUTTING THE APPLET CODE INTO YOUR HTML PAGE
Now comes the easiest part. To insert applet code into an HTML page, you
simply copy everything from <APPLET> through </APPLET> into the
<BODY> area of your page HTML, wherever you would like the applet to
appear.
To make this clearer, you can think of everything from <APPLET> to
</APPLET> as one block, and insert the whole block into your page just
like you would, say, an image <IMG> tag.
For layout control, note that you can put the entire <APPLET> ...
</APPLET> block into an individual cell in a table; as in this example,
which would show a 5-pixel red border around the applet:
<table cellpadding="5" bgcolor="#FF0000">
<tr>
<td>
<APPLET CODE="filename.class" WIDTH="400" HEIGHT="200">
<PARAM NAME="SPEED" VALUE="100">
<PARAM NAME="IMAGE1" VALUE="thisimage.gif">
<PARAM NAME="IMAGE2" VALUE="thatimage.jpg">
</APPLET></td>
</tr>
</table>
Tip: Note how we pulled the </td> tag right against the
</APPLET> tag, by the way -- that solves a shortcoming of Netscape
when it comes to proper centering.
GETTING IT ALL TO YOUR SERVER
For newcomers to JAVA, always put everything related to the JAVA applet in
the same place (directory) on your server.
What has to go up? Your HTML page, of course. But also be sure any image
files or other resources the applet uses are properly sent up. If they aren't
there, most applets will be unable to start.
Finally, and of special note, the applet class file must be sent up. We can't
say it often enough: Make sure the class file is on the server, where it
should be, and make certain you sent it up in binary format.
A QUICK TAKE ON TROUBLESHOOTING
The single most common cause for problems with JAVA applets is either neglecting
to send up the class file, or sending it up in ASCII (text) format, instead
of binary.
If you see an error in the browser status bar like "class whatever.class
not found" or "class whatever.class could not be loaded", send the class
file up again, and watch your FTP client to be sure it goes up in binary.
The second most common problem is forgetting to send up resources the applet
needs, such as image files or text files. Obviously, make sure they're on
the server, in the right place, and sent in the appropriate formats.
The third most common problem is, you didn't proofread your code! Remember,
check it three times, and when you're absolutely sure it's right? Check it
again.
LAST WORDS...
There is a truly stunning array of JAVA applets available, thousands upon
thousands of them, that perform myriad tasks and functions -- from basic
text scrollers to striking display applets and powerful animation tools.
Once you get the basic dance steps down, JAVA applets are straightforward
installations, and a great way to add interest, functionality, and versatility
to your pages.
Even better, many JAVA applets are absolutely free for the taking, so don't
miss a great opportunity to add some flash for zero cash!
About the Author:
David Sosnowski is the webmaster of CodeBrain.com Free Java at
http://www.codebrain.com
. He is also the creator of CodeBrain.com's many freeware and commercial
applets, kits, JavaScripts, and Perl scripts.
|
|