import java.applet.Applet;
import java.applet.AppletStub;
import java.awt.Label;
import java.awt.Graphics;
import java.awt.GridLayout;
public class PreLoader extends Applet implements Runnable, AppletStub {
String appletToLoad;
Label label;
Thread appletThread;
public void init() {
// This is the applet we want to delay
loading
appletToLoad = getParameter("applet");
label = new Label("Stay tuned" + appletToLoad);
add(label);
}
public void run(){
try {
// Get the class
of the real applet
Class appletClass
= Class.forName(appletToLoad);
// Create an instance
of the real applet
Applet realApplet = (Applet)appletClass.newInstance();
// Set the stub
for the applet
realApplet.setStub(this);
// Get rid of
the Stay Tuned message
remove(label);
// Maximize the applet
to the current size
setLayout(new GridLayout(1,
0));
add(realApplet);
// Lets now display
the real applet
realApplet.init();
realApplet.start();
}
catch (Exception ex) {
// Display an error
label.setText("Error loading
applet.");
}
// We need to redraw the screen
validate();
}
public void appletResize(int width, int height) {
// Transfer the call of
AppletResize from the stub to the applet.
resize(width, height);
}
}
Listing 2
GraphicsDevice aDevice;
Windows aWindow;
DisplayMode newDisplayMode;
DisplayMode oldDisplayMode = aDevice.getDisplayMode();
try {
aDevice.setFullScreenWindow(aWindow);
aDevice.setDisplayMode(newDisplayMode);
// do rest of work
}
finally {
aDevice.setDisplayMode(oldDisplayMode);
aDevice.setFullscreenWindow(null);
Listing 3
public boolean imageUpdate(Image img, int flags, int x, int y, int w, int
h) {
if ((flags & ALLBITS)
!=0 {
repaint();
}
else if (flags &
(ABORT | ERROR)) != 0) {
error
= true;
//
File was not found so consider displaying placeholder
repaint();
}
return (flags
& (ALLBITS | ABORT | ERROR)) == 0;
}
Listing 4
public static void runOnce(String id, final long milliseconds) {
synchronized(s_queue)
{ // s_queue is a collection of all events
if
(!s_queue.containsKey(id)) {
s_queue.put(token,
new Latest());
}
}
final Latest
latest = (Latest) s_queue.get(token);
final long
time = System.currentTimeMillis(); // Get current time
lastest.time
= time;
(new Thread()
{public void run() {
if
(milliseconds > 0) {
try
{Thread.sleep(milliseconds);} // Sleep for a little bit
catch
(Exception ex) {}
}
synchronized(lastest.running)
{ // Wait until previous finishes
if
(latest.time != time) // Only run latest
return;
}
}}).start();
}
private static Hashtable s_queue = new Hashtable();
private static class Latest {
public long time=0;
public Object running = new
Object();
}