Java for C# Programmers 8: Applets

Java Applets

A Java applet is a Java program that is usually run in a web browser. The Java bytecode is hosted on a web server and delivered to the user. In the user’s browser, the applet is run by a JVM which is part of the browser, usually in form of a plugin.

There are these main differences to a normal Java app.

  1. The main class must be derived from java.applet.Applet or, for a Swing applet from javax.swing.JApplet.
  2. There is no static void main(..) function. Instead, an applet has several functions that can and should be overridden by your applet. They are explained in the template below.
  3. No size inside the applet. Size is taken from HTML.
  4. An applet is closed, when the browser (or tab) is closed.
  5. An applet can not have a title, only the website can have it.
public class AppletTemplate extends Applet  // or JApplet
{
    // Is called once when the applet is first loaded 
    // into the browser.
    public void init() { }

    // Is called after loading of the applet, whenever the 
    // applet becomes visible. It then should start its execution.
    public void start() { }

    // Is called when the applet becomes invisible, e.g. because some 
    // other window is put in front of the applet. The applet then 
    // should stop its execution.
    public void stop() { }

    // Is called, when the applet is removed from memory. An applet
    // that uses resources must free them here.  
    public void destroy() { }

    // Is called when something shall be painted. 
    public void paint (Graphics g) { }
}

How to Test Your Java Applet Locally

It is easy to test it via Eclipse. When the active window contains an applet and you start debugging, automatically Eclipse’s applet viewer starts and shows the applet.

But at some point, you’ll probably want to see your applet running in a browser.

To be able to view an applet in the browser, a link to the applet has to be put into some html code. You can use the deprecated applet or the newer object tag. An example for both follows. The applet class here is ColorMixerClass in package Applets.


    
        

ColorMixer Java Applets

Gotcha: There are some minutiae which you need to know.

  1. Use a fully qualified URL-like path in the archive parameter, with leading file://. Just giving C:\Data\Java-works\bin\ColorMixer.jar in the example above will lead to an IllegalArgumentException: name.
  2. In the code or classid parameter, give the full path of the applet class inside the jar. Otherwise you’ll get an ClassNotFoundException
  3. Astounding, but true: Some of the latest Java update put a higher security restriction on applets coming from the local file system, than it does for applets from the internet.
    So even if you can run Java applets from the internet in your browser, it is probable that you cannot run the applet that you’ve written yourself and that is located on your local harddrive. If that is the case, you need to lower your computer’s security settings.

    1. Go to your Control Panel/Java/Configure Java/Security and put the security to Medium.
    2. After changing the security level, you might need to end some hanging browser and/or Java processes until you can get your applet to run.