With every applet I’ve ever written (so far as I can recall), I’ve had the following problem:
It runs fine the first time. Then, if I open it again in the same Internet browser when any window of the Internet browser is open, the applet is just empty space. This didn’t happen with an applet I checked that was written by some else, so I assume it’s a problem with my code.
Shutting all the windows in the Internet Browser and then opening up the page with my applet again works fine. I assume that Internet Browsers cache the applet somehow, but this doesn’t cause a problem with other people’s code.
I’m using Java 5, but I’ve always had this same problem. This occurs with both Mozilla Firefox and Microsoft Internet Explorer.
Here’s the parts of my code that I believe are relevant:
/** <p>This class runs the Nanotron applet.
@author Steven Fletcher
@since 2006/07/10
@version 2006/08/02*/
public class Nanotron extends JApplet implements Runnable {
//APPLET/////////////////////////////////////////////////////////////////////////////////
/** Destroys this applet.*/
public void destroy() {
//stop the main loop
synchronized(this) {
bDestroyApplet = bPauseApplet = true;
}
} //end destroy
/** Initializes this applet.*/
public void init() {
//do some initialization
//...
} //end init
/** Starts this applet.*/
public void start() {
//focus on this applet so that keyboard input is accepted
requestFocus();
//unpause the main loop
synchronized(this) {
bPauseApplet = false;
}
//start the main loop
//NOTE: The main loop can't be started during init or start will never be called.
new Thread(this).start();
} //end start
/** Stops this applet.*/
public void stop() {
//stop the main loop
synchronized(this) {
bPauseApplet = true;
}
} //end stop
//RUNNABLE///////////////////////////////////////////////////////////////////////////////
/** Executes the game's main loop.*/
public void run() {
while(true) {
//check for the end of the loop
synchronized(this) {
//do nothing while the applet is paused
while(bPauseApplet) {
//if the applet should be destroyed, stop
if(bDestroyApplet)
return;
//sleep
try {
Thread.sleep(100);
} catch(InterruptedException exception) {}
}
};
//do the actual loop
//...
} //end while forever
} //end run
//VARIABLES//////////////////////////////////////////////////////////////////////////////
//multithreading
private boolean bPauseApplet = false, bDestroyApplet = false;
} //end class Nanotron