Main Page

Previous Next

More on Applets

Applets are a peculiar kind of program as they are executed in the context of a web browser. This places some rather severe restrictions on what you can do in an applet, to protect the environment in which they execute. Without these restrictions they would be a very direct means for someone to interfere with your system – in short, a virus delivery vehicle.

System security in Java programs is managed by a security manager. This is simply an object that provides methods for setting and checking security controls that determine what is and what is not allowed for a Java program. What an applet can and cannot do is determined by both the security manager that the browser running the applet has installed, and the security policy that is in effect for the system.

Unless permitted explicitly by the security policy in effect, the main default limitations on an applet are:

  • An applet cannot have any access to files on the local computer.

  • An applet cannot invoke any other program on the local computer.

  • An applet cannot communicate with any computer other than the computer from which the HTML page containing the applet was downloaded.

Obviously there will be circumstances where these restrictions are too stringent. In this case you can set up a security policy that allows certain operations for specific trusted programs, applets, or sites, by authorizing them explicitly in a policy file. A policy file is an ASCII text file that defines what is permitted for a particular code source. We won't be going into details on this, but if you need to set up a policy file for your system, it is easiest to use the policytool program supplied with the JDK.

Because they are normally shipped over the Internet as part of an HTML page, applets should be compact. This doesn't mean that they are inevitably simple or unsophisticated. Because they can access the host computer from which they originated, they can provide a powerful means of enabling access to files on that host, but they are usually relatively small to allow them to be easily downloaded.

The JApplet class includes the following methods, which are all called automatically by the browser or applet viewer controlling the applet:



void init()

You implement this method to do any initialization that is necessary for the applet. This method is called once by the browser when the applet starts execution.

void start()

You implement this method to start the processing for the applet. For example, if your applet displays an animated image, you would start a thread for the animation in this method.

This method is called by the browser immediately after init(). It is also called if the user returns to the current .html page after leaving it.

void stop()

This method is called by the browser when the user moves off the page containing the applet. You implement this to stop any operations that you started in the start() method.

void destroy()

This method is called after the stop() method when the browser is shut down. In this method you can release any resources your applet uses that are managed by the local operating system. This includes such things as resources used to display a window.

These are the basic methods you need to implement in the typical applet. We really need some graphics knowledge to go further with implementing an applet, so we will return to the practical application of these methods in Chapter 13.

Converting an Application to an Applet

Subject to the restrictions described in the previous section, you can convert an application to an applet relatively easily. You just need to be clear about how each part of program executes. You know that an application is normally started in the method main(). The method main() is not called for an applet but the method init() is, so one thing you should do is add an init() method to the application class. The other obvious difference is that an applet always extends the JApplet class.

We can demonstrate how to convert an application so that it also works as an applet, by changing the definition of the Sketcher class. This doesn't make a very sensible applet, but you can see the principles at work.

Try It Out – Running Sketcher as an Applet

You need to modify the contents of so that it contains the following:

// Sketching application
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JApplet;

public class Sketcher extends JApplet {
  public static void main(String[] args) {
    theApp = new Sketcher();              // Create the application object
    theApp.init();                        // ...and initialize it

  public void init() {
    window = new SketchFrame("Sketcher");        // Create the app window
    Toolkit theKit = window.getToolkit();        // Get the window toolkit
    Dimension wndSize = theKit.getScreenSize();  // Get screen size

    // Set the position to screen center & size to half screen size
    window.setBounds(wndSize.width/4, wndSize.height/4,        // Position
                     wndSize.width/2, wndSize.height/2);       // Size


  private static SketchFrame window;              // The application window
  private static Sketcher theApp;                 // The application object

To run Sketcher as an applet, you should add an .html file to the Sketcher directory with the contents:

<APPLET CODE="Sketcher.class" WIDTH=300 HEIGHT=200>

If you recompile the revised version of the Sketcher class, you can run it as before, or using AppletViewer.

How It Works

The class now extends the class JApplet, and an import statement has been added for the javax.swing package.

The init() method now does most of what the method main() did before. The method main() now creates an instance of the Sketcher class and stores it in the static data member theApp. The method main() then calls the init() method for the new Sketcher object. The window variable no longer needs to be declared as static since it is always created in the init() method.

The class member, theApp, must be declared as static for the case when the program is run as an application. When an application starts execution, no Sketcher object exists, so the data member, theApp, does not exist either. If theApp is not declared as static, you can only create the Sketcher object as a local variable in main().

Even if Sketcher is running as an applet, the application window appears as a detached window from the AppletViewer window, and it is still positioned relative to the screen.

Of course, when we come to implement the File menu, it will no longer be legal to derive the Sketcher class from the JApplet class since it will contravene the rule that an applet must not access the files on the local machine. It is also not recommended to create frame windows from within an untrusted applet, so you will get a warning from the appletviewer about this.

All you need to do to revert back to just being an application is to remove the import statement for javax.swing and remove extends JApplet from the Sketcher class header line. Everything else can stay as it is.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor