Slick2D: Deploying a LWJGL Applet

Java Applet Pains

I recently had the pleasure of building a low-fi promotional video game. One of the requirements? The game needs to deploy as an in-browser Java Applet. When was the last time you created one of those?

After doing some research, I decided to utilize the Slick2D library, a dated but effective wrapper around the Lightweight Java Gaming Library.

LWJGL “nicely” allows you to deploy your game in different ways, while not drastically changing your game’s codebase. From the browser-based applet, standalone .jar executible, to the .jnlp web-start application; however, each has it’s own little quirks and frustrations.

I recall spending an evening completely befuddled as to why my game wasn’t deploying properly to the browser. I could run other java applets (a separate headache on linux, for sure) and followed any tutorial I could find on deploying applets. Key-signing files. You name it … futile.

Finally, I stumbled upon a combination that worked:

  • Make sure your main Slick2D class driving your game is called “MyGame”.
  • Deploy.js can be found here. It’s Sun’s helper library.
  • The ‘gamelib’ is a child-level directory of your game project where your lwjgl libs are likely stored.
  • Incorporate the snippet below, and load in your browser.
Deploying Java Applet w/ Slick2D & LWJGL - index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<html>

<script type="text/javascript" src="./gamelib/jquery-1.7.1.min.js"></script>
<script src="./gamelib/deployJava.js"></script>

<script>
    var attributes = {codebase:'./gamelib/',
                      code:'org.lwjgl.util.applet.AppletLoader',
                      archive:'lwjgl_util_applet.jar, lzma.jar',
                      width:'100%', height:'97%'} ;

    var parameters = {
      al_main:'org.newdawn.slick.AppletGameContainer',
      game:'your.game.package.MyGame',
      al_jars:'slick.jar, mygame.jar, lwjgl.jar.pack.lzma, jinput.jar.pack.lzma, lwjgl_util.jar.pack.lzma',
      al_windows:'windows_natives.jar.lzma',
      al_linux:'linux_natives.jar.lzma',
      al_mac:'macosx_natives.jar.lzma',
      al_cache:'false' } ;

    var version = '1.6';

    $(document).ready(function(){
          deployJava.runApplet(attributes, parameters, version);
    });

</script>
<body>
</body>

</html>

Comments