Scripting Applets Pays Off
By Al Williams
When you want to add client-side functionality to a Web site you have to make a choice: applets or JavaScript. Then again, do you? Java applets are powerful, but compared to JavaScript they're hard to develop and modify. On the other hand, some jobs are tough to code using script.
Why not partition your task so you can use an applet in conjunction with a script? Then you can incorporate the best parts of each technology.
To illustrate this idea, I've put together an applet designed to work with a script. The applet simulates a pair of dice. Since that's all it does, you could use it for a variety of Web sites. The example this month is a simple craps game (see
Figure 1). However, you could easily create a variety of casino games, board games, or even role-playing games. If you don't like games, you can apply the same techniques to more serious applets.
Besides communicating with a script, the example applet also illustrates other interesting techniques. For example, the applet has to synchronize two threads and generate random numbers.
The Power of Applets
There are several reasons to roll functions into an applet:
- Some functions are easier to create in regular Java than in JavaScript.
- It may be possible to perform operations in Java that would otherwise require capabilities (like DHTML) that are not as widely supported as Java applets.