Java Applets

Applets are "mini applications" written in Java that can be embedded in a web page or, in principle, other program. And in reality, the most common case is to embed them in a web page. Writing an applet is largely similar to writing a normal application (and with not much effort, you make most applets work as normal applications too). However, there are also some differences to bear in mind:

If you essentially know what an applet is and just want to start writing one, then you may want to skip straight ahead to the next section, which gives a skeleton Java applet and discusses how to build on it.

When to use applets?

The above points mean that applets are great for certain things and unsuitable (or akward to implement) for other things. I personally think they're most at home when you need an interactive part of a web page that involves some degree of logic or moderately complex user interface. Writing your component as an applet gives you the benefits of having a "full" object-oriented language, a moderately rich user interface library in the form of Swing, and indeed pretty much all of the standard Java platform (including things such as Javasound and the Java concurrency library).

If your component is just, say, a simple animation, you may find that other tools such as Flash are more appropriate. On the other hand, so long as you're prepared to program it, you can do some quite complex graphics and animation with Java applets— after all, you have the whole Java2D framework at your disposal.

To give you a small taster of some simple applets, here are some that I use on some of my web sites:

If you look on the web, you'll find plenty of other examples, ranging from physics simulations to spectrum emulators to new feeds.

Notice a technique used by various of these applets and extremely usedful for deployment in web sites, in which the same applet is configured to behave differently on different pages. As you page through the Spanish tutorial, for example, you'll see that each page practises some different language feature. But in reality, the applet code is the same each time; the web page contains parameters passed into the applet to indicate how it should behave. The same is true of the wordsearches and crosswords: the particular words to use on that page are passed into the applet as parameters.

When not to use applets?

In practice, Applets are unsuitable for more "heavywiehgt" applications: essentially where an application needs to override one of the restrictions mentioned above. For example:

Next: skeleton applet code

As mentioned, the next page introduces the JApplet class, which is where some of the Applet "housekeeping" code lives. We present a skeleton applet, and then discuss how to expand it.

If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.