Java Applets

Site Map Distance LearningHome Up Getting Started Using Graphics Various Browsers Forms Java Applets WebTV Users

horizontal rule

 Java is a programming language that can be used to create both standalone programs (.exe files) and programs that run in web pages (.class files). The web based programs are called Applets.

Inserting Applets Into Web Pages

 Inserting applets into your web pages involves specifying the following information.
bulletName of the applet class file

bulletSize of the applet

bulletPath to the folder containing the applet (if that folder is not the folder that contains the index.html file)

bulletAny parameters the applet needs

bulletXHTML code for browsers that do not support Java
For example, at the bottom of this page is an applet version of the Lunar Lander game. The XHTML code that inserts that applet into this page is the following (the code should be in one line):
<applet CODE="JDLunarLander.class" WIDTH="550" HEIGHT="250" align="center" codebase="lander/">
[If your browser supported Java, you'd be able to play the Lunar Lander game!]
That code uses the following reserved words to specify the necessary information.
bulletAPPLET and /APPLET. Beginning and ending of the applet

bulletCODE.  Name of the applet class file

bulletWIDTH and HEIGHT. Display size of the applet in the web page

bulletALIGN. Center the applet in the web page

bulletCODEBASE. Path to the folder containing the applet. If the applet is in the folder containing the XHTML page that contains this XHTML code, this reserved word can be omitted.

bulletXHTML tags and text for browsers not supporting Java can be inserted anywhere within the applet. They are ignored by the java system, but they are used if java is not running.
This particular applet did not need any parameters. If parameters were needed, they would be specified with a PARAM tag.

If you get applets from other designers, the applets should come with documentation that gives the XHTML code to be used to insert the applets in web pages. Be aware that you may use different folders and will have to modify the paths to the folders accordingly. When downloading the class files, do a shift-click on the download links so you will download the .class files and not the .exe files. If you download .zip files, you will have to unzip them.


The Lunar Lander applet that is installed in this page uses a CODEBASE in its XHTML code. The CODEBASE parameter tells the browser where to find the applet. Here are suggestions about using CODEBASE.

bulletIf the XHTML code uses CODEBASE, then place the applet in a folder that is inside the folder containing the page that has the HTML code. The applet is one level down from the page that activates the applet.

 The applet installed in this page is inside a folder called lander. That folder is inside the main project folder, as is this page. The CODEBASE tells the browser it will find the applet (.class file) inside the lander folder. Notice that in the XHTML code there is a forward-slash after the name of the folder (lander/).

bulletIf the XHTML code does not use CODEBASE, then don't add CODEBASE to the XHTML code. Put the applet (.class file and any supporting files) inside the same folder that has the page containing the XHTML code.

For example, suppose you have a page in your web site that is called plants.htm  You want that page to activate an applet. The XHTML code for the applet does not contain a CODEBASE. You could organize your files as follows.

bulletPlace the plants.htm file in your main project folder, as you normally would.

bulletPlace the applet files in a folder called plantsapplet and place that folder inside the main project folder. By placing the applet in its own folder, you can easily remove or replace it if you want to.

bulletTo avoid using a CODEBASE, since the XHTML code does not use one, create a new page called plantsapplet.htm and place that page inside the plantsapplet folder. Paste the HTML code for the applet in this page.

bulletIn the plants.htm page, place a link to the plantsapplet.htm page. Clicking the link will activate the applet.

An alternate way would be to place the applet directly in the main project folder, and paste the HTML code into the plants.htm file.

A Caution!

Not all browsers can handle Java, and not all people who have Java-capable browsers surf with Java enabled.  Thus, before you use Java in your web site, you need to be satisfied that your intended audience will be able to see your applets.

Lunar Lander Game

[If your browser supported Java, you'd be able to play the Lunar Lander game!]

Use the space bar for the landing thruster and the left/right arrow keys to spin the lander. Try to land on the blue landing pads. If you land successfully, you get half your fuel back. Change the gravity to make it harder

This game is complements of John Donohue

Site Map ] Distance Learning ]Home ] Up ] Getting Started ] Using Graphics ] Various Browsers ] Forms ] [ Java Applets ] WebTV Users ]

horizontal rule



Copyright 1998, 2011 Allen Leigh