Creating Slick2D Applets

Introduction
Once you have created a Slick2D application you can very easily use it as a Java applet. This is done without changing or adding any Java code and done completely through HTML. Slick2D uses LWJGL's AppletLoader to deploy the applet in the browser and then uses Slick2D's AppletGameContainer to run as an applet.

Getting Started
Easiest way to get started is to grab the LWJGL Applet package from the LWJGL download page and modify it.

Basic Example
A basic example of slick applet html tag will looking something like the following

<applet code="org.lwjgl.util.applet.AppletLoader" 
        archive="lwjgl_util_applet.jar" 
        codebase="." 
        width="640" height="480">
 
  <param name="al_title" value="myslickgame"> 
  <param name="al_main" value="org.newdawn.slick.AppletGameContainer"> 
  <param name="game" value="org.mygamepackage.MyGameMainClass">
 
  <param name="al_jars" value="mygame.jar, lwjgl.jar, slick.jar"> 
 
  <param name="al_windows" value="windows_natives.jar"> 
  <param name="al_linux" value="linux_natives.jar"> 
  <param name="al_mac" value="macosx_natives.jar"> 
 
  <param name="separate_jvm" value="true"> 
</applet> 

To use the AppletGameContainer you specify it as the main class in the al_main parameter (remember not to use it in your Java code which can continue using AppGameContainer).

You must also tell Slick2D what your main game class is, this is done using the game parameter.

You should export your application code (class files) into a jar.

All the jars your application uses including slick.jar should go in the al_jars parameter.

Signing Jars
When using slick, you shouldn't have to sign any jars and can safely just use the pre-signed lwjgl jars to run the applet, however if your application requires any access outside the java sandbox then you must sign the jars that require this. If your signing your own jars then you should clear out the existing certificates from the jars (just delete the META-INF folders in the jars and resign) as multiple certificates are not recommended and just lead to multiple security dialogs.

Finally
You should read the tutorial on Deploying with the LWJGL AppletLoader to get a better understanding of how it all works.

Notes
Do note when using AppletGameContainer the main method of your slick application is not run, so if you have any code that you wish to initiate at the start of your applet you should move it to the init() method.

The Slick2D API does not expose any specific features of the different GameContainer's. You can however use the container object provided by the init(), update() and render() methods to get these:

To check if Slick2D is running as an applet use the following.

if (container instanceof AppletGameContainer.Container) { 
    // do foo 
}

To get parameters that have been passed to the Slick2D Applet, you simply obtain the Applet instance and then use its getParameter(String) method.

// get the parameters by casting container and getting the applet instance 
Applet applet = ((AppletGameContainer.Container) container).getApplet(); 
applet.getParameters(String); 

Comments

Thanks so much for this! It helped me out a lot, but I still have one (or maybe more) problem. I signed my jars but I still get this error:

access denied (java.io.FilePermission /Users/Johnny/Library/Caches/Java/cache/lwjglcache/localhost/game read)
java.security.AccessControlException: access denied (java.io.FilePermission /Users/Johnny/Library/Caches/Java/cache/lwjglcache/localhost/game read)

Any help will be greatly appreciated. Thanks again for reading this! :D

Anonymous Ninja
Sat, 09/15/2012 - 03:01