Applet Help

CS1322 might start offering more applet coverage based on popular demand. CS1502 did not include Applets in our standard curriculum because they are Java-specific. Around the end of each quarter, though, we always got questions on how to convert graphical applications to Applets. It's a very simple task.

The Basics of Applets

Applets, part of the Java package java.applet, are simply an extension of awt.Panel. They can do everything a Panel can do. To make a class an Applet, simply extend Applet. To make an Applet functional, you need to implement the init() method, and possibly three others:

public void init()

This method is analagous to main() in graphical applications. This is the entry point into the program. Do your instantiation of classes here.

public void start()

This is the method called right after init when the applet is started. This is traditionally where any threads are started. If you don't have any threads, you can omit this method.

public void stop()

This is called when a user clicks "Back" on their browser. The applet still "exists", but should go to a dormant state. This is generally where threads are paused. If you don't have any threads, you can omit this method.

public void destroy()

This is called when the Applet is cleared from the user's cache (browser was closed or Applet just flushed out). If you have any large memory resources, you can clean them up manually, or just let the browser do it. You can safely omit this method, though, since browsers can generally manage their own garbage collection.

You may include as many other methods in the class extending Applet if you wish. Generally, this should be just a starter-up class, though. Here is a very simple Applet class:

import java.applet.Applet; // import the Applet classes
public class myApplet extends Applet {

    public void init() {
   new MyFrame();
   }
}

In this example, all the Applet did was start an instance of myFrame, which might be part of an existing program you've already written.

HTML Integration

Modern browsers deal with Applets similarly to images. They will place them on the page with the same logic as an image would be placed. All you need to do to add an Applet to a page is add the <APPLET> and </APPLET> tags to your HTML document.

For instance:

<HTML>
<BODY>
<B> This is standard bold text</B>
<APPLET code="myApplet.class" height=200 width=200>
</APPLET>
</BODY>
</HTML>

There are many more parameters you can add to the <APPLET> tag, but CODE, HEIGHT, and WIDTH are the important ones. Don't forget to close the APPLET tag.

Setting the background color

Most browsers make grey the default background color of Applets. But because Applets have all the properties of Panels, we can easily set the background color to white or anything else we want:

myApplet.setColor( Color.white );

Coder Beware

There are many things that Applets cannot do, or will do with warnings.

Frames: The first and most obvious is opening up Frames. You can bring up Frames (or more generally, Windows) just as you do in any other Java program, but all Frames created in an Applet are considered "unsigned". This means that no program officially owns them, so they are a security risk, but a low level of one. This is no big deal, but you will get a warning label on all Frames created with an Applet telling you "Unsigned Frame" or something similar. However you can get applets to talk across Frames to other applets, or to javascript.

Files off of the web server: To get files off of the web server on which the Applet resides, you cannot use simple File I/O. The easiest way is to use the methods in Applet:

public Image getImage ( URL myurl )
public AudioClip getAudioClip ( URL myurl )

These methods can return images and sound files off of the web server (or any web server for that matter). Remember that these are not static calls, so your instance of Applet must fetch these files for you.

Files off of the local machine: Not gonna happen. Considered the highest level security risk, so applets are not allowed to see the local hard drive.

Local IP address: Also not gonna happen. It is considered a security risk for an Applet to know the IP address of the browser that is running it. This makes network sockets impossible to set up through Applets.

System.out.println: No can do. Printing to stdout is prohibited, since there is no standard place for text output. Well, actually, SOP goes to the Java Console. In Netscape it follows the following path:

Communicator-->Tools-->Java Console

Since we are confessing, it should be noted that you can do:

myApplet.showStatus( strText );

which will place a text String in the status bar of the browser. That's the closest replacement.

What about JApplets?

JApplets are part of the Swing packages of Java 1.2/2.0. Swing and JApplets will work in all browsers that support plugins. The only difference is that the <applet> tags must be replaced with <embed> tags, and the user will need to load the plugin.

If you are interested in using them, they work the same way, except you replace references to Applets with JApplet and references to java.applet with javax.swing.

A word of caution/advice before you jump into creating thousands of applets, most browsers do not have wide support for Java versions greater than 1.1.