I wrote a demo Applet and tried to break it…
Using Appletviewer, isActive() is still false about 50% of the time on entry to run(); Internet Explorer, Firefox and JNLP Applet seem to set isActive() earlier. Adding a yield() before checking isActive() improves matters, although sometimes it takes more than one yield(). Adding while(!isActive()) Thread.yield(); at the start of run() makes AppletViewer work 100%, but is it worth the overhead to fix what is predominantly an Appletviewer problem.
If you grab the Graphics context on Internet Explorer at the start of run(), it doesn’t return null (so no exception), but it isn’t linked to the native peer, so nothing displays. Also Internet Explorer changes the device context everytime you minimise and then maximise the browser. Thus we must do getGraphics() inside the game loop. While you can skip the dispose(), this means that unreleased graphics contexts build up until a garbage collection occurs. The question is whether it is worth including the overhead of dispose() to add robustness to the applet.
Lastly, if the browser navigates away from the page containing the applet or you close the browser, then the graphics context obtained at the start of the loop becomes null, even if it wasn’t null when you performed getGraphics(). The probability of this can be reduced by postponing getGraphics() until just before you need it, testing for null and only drawing on the device if it’s valid. However I found that occasionally, even this isn’t sufficient as windows manages a context switch between the test for null and the draw. i.e. the operation is not atomic. Using try {…} catch (Exception e){} around the draw routine catches the exception, but costs more bytes. The question is whether it is worth adding the overhead of try/catch to catch an exception that only occurs when the user is navigating away from your applet. As far as I can see this error doesn’t actually matter, provided you recreate the Thread when the page is revisited. This suggests creating the thread in start() and terminating it when isActive() goes false is prudent. I’m tempted to omit the try/catch and accept the exception occurs.
If we don’t try to catch exceptions when the graphics context goes null, it becomes more important to postpone grabbing the context to as late as possible in the main loop, just in case some browser returns null the first time through the loop. I haven’t seen this, but haven’t tried every possible browser.
Thus there are tradeoffs between robustness and code size. If you don’t care about appletviewer and null pointer exceptions, then IMO a minimum size template would: create the thread in start(); grab the graphics context as late as possible within the game loop and use it immediately; testing for inActive() at the end of the game loop and terminating the thread if it is false. If the game spends a fair bit of time initialising it may even work in Appletviewer.