Applet Code Tip
For today I have a simple coding tip to keep in mind when writing applets and it is this:
As much as possible do not use methods of the Applet (JApplet) class.
This seems at first glance to be an odd thing to suggest but actually it isn't. With the exception of life-cycle related events like bootstraping via init() you can avoid using Applet methods in general and your code will become portable to non-applet applications because of it.
As an example of what to avoid, there are a number of Applet and AppletContext specific methods like getImage and getStream which are easily replaced using techniques available to any Java program, stand-alone or applet.
So instead of usingInputStream in = getAppletContext().getStream(key);
useInputStream in = getClass().getClassLoader().getResourceAsStream(key);
It seems to me that one of the main advantages of applets is that you can re-use code from them to stand-alone applications and vice-versa, so while there are a number of these "convenience" methods in Applet why not just simply use methods that will work both inside and out of an Applet?
|© 2008 Max Stocker|