tropo://techno/java/preloader |
Java Preloader Secrets |
This is the Java Class Preloader that is used to "preload" java
classes before you think they'll be used. It is typically
used when the user is reading one web page and you
think (hope) they'll follow a link to another page with
an applet - well then on the first page you run the preloader
and then if they spend enough time on the first page then
by the time they get to the second, voilla, all the classes
will already be loaded and the applet will "load"
instantaneously. It works by calling
Class.forName()
on each class in a configurable list of classes -- this causes the
class files to be loaded by the browser and then they're cached
when they're later needed.
This is used on our Colour Explorer intro page to load classes just before they're used when the Colour Explorer really runs.
The source is in one file, TRPreloaderApplet.java and when compiled the bytecodes are only 1,700 bytes long. There is also a bit of javadoc on this too.
In a web page it's used as follows. Note that the
width
and height
are set to 1
since we don't want this to be visible.
|
This is how it's used on on the Colour Explorer intro page - classes are loaded in reverse order, though in retrospect that may not be necessary - but this is the most bombproof way to guarentee that they all load.
|
And just for completeness, the standard HTML trick of preloading images is done via putting something like this:
<img src="big.gif" width=1 height=1>
on a page before "big.gif" is needed - then
when it is later loaded it will appear instantly as it will be in the
browser image cache. There doesn't appear to be any need to have the
preloader applet load images as this image trick should do the necessary
cache loading.