Lesson 1.5 Java Applets
Java Applets
This section is a little shorter than the others in this lesson. We won't actually start learning to program in Java until the next lesson. You'll learn to use HTML to add a Java applet to a Web page.
What are Applets?
Java applets are compiled Java programs that are stored on a Web server. A Java applet is a special kind of program because it can't run on its own. It needs a Web browser to give it a home. When your Web browser encounters a reference to a Java applet in a Web page, it sends a request to the server that gave it the Web page to send along the compiled code for the applet as well.
This works just the same way images work, but not the way that JavaScript or other scripting languages work. In those languages, the actual source code for a program is written in a Web page. When your browser downloads a page containing JavaScript, your browser itself compiles the JavaScript code and then executes it.
With Java applets, when your Web browser receives the compiled Java program from the Web server, it launches a Java Virtual Machine [this is the interpreter that will run the Java program], provides a bit of screen real estate for the applet to use, and then lets the applet start running.
Applets and HTML
Like everything else in HTML, you use a tag to include a Java applet in your Web page. The tag is called, appropriately the <applet> tag. Here are the basic pieces. We'll cover some other attributes and tags (such as <param>) as we go through the semester.
|
Code
|
Example
|
Description
|
Required?
|
| <applet |
|
Opening tag |
Yes
|
| code= |
"Myapp.class" |
File name of applet |
Yes
|
| height= |
100 |
Height allowed [in pixels] |
Yes
|
| width= |
300 |
Width allowed [in pixels] |
Yes
|
| codebase= |
"/classes/" |
URL where file located |
No
|
| > |
|
Close opening tag |
Yes
|
| </applet> |
|
Closing tag |
Yes
|
A Few Examples
Here are HTML examples for adding applets to your page.
You probably don't want to add several applets to a single page, as this lesson does. Each applet uses up memory, and adding several can make the page very slow to load.
Example 1: Use a regular hyperlink
If you simply want to link to an applet running on another page, then use a regular HTML hyperlink. You don't need to use the <applet> tag at all.
For instance, you might want to check out Ben Permadi's Fractal Painter, which is an amazingly addictive Java applet. Ben requests that you link to his page rather than using the binary Java class files on your own page. In both cases, however, the actual binary Java programs are downloaded to the client of the person who views them.
The URL for Fractal Painter is:
http://www.permadi.com/java/spaint/spaint.html
Example 2: Link to a "local" applet
If the .class file for an applet is in the same directory as the HTML file that hosts it, you only need supply arguments for CODE, HEIGHT, and WIDTH. You don't need to use CODEBASE.
The applet tag looks like this:
<applet code="Bubbles" width="485" height="124"></applet>
Notice that the code attribute does not have to have the class extension. If you don't add an extension, then .class is assumed. (Of course, the actual binary Java file must have the .class extension.)
Example 3: On your server in another directory
If the .class file for an applet is in another directory from the HTML file that hosts it, you use the codebase attribute, but use a relative URL.
The actual [binary] code for David Krider's BallDrop applet is located in a directory called classes, located below the directory holding this HTML page. The code for inserting it in a web page is:
<applet codebase="classes" code="BallDrop.class" width="300" height="300"></applet>
Note: The applet is available as a free download.
Example 4: Applet on another server
If the compiled [class] files for a Java applet are on another server, you can still include that applet in your own Web page. You do that by setting the codebase attribute to point to the URL of the directory where the class files are stored. Note that when you do this, the applet may move at any time.
In the example below, the name of the class is centipedo.class, and the class file is located on a directory somewhere out on the Internet. Note that, as with all HTML, it makes no difference whether you put CODEBASE or codebase, or HEIGHT or height. The same is not true of the arguments to the CODE attribute, however. If you type CODE=CENTIPEDO instead of CODE=centipedo, the Web server will not give you the applet.
<applet
codebase=http://javaboutique.internet.com/centipedo/classes code=centipedo.class
width=500
height=448>
</applet>
Please continue to the next section of this lesson.
|