WormChase as an AppletFigure 3-1 shows the WormChase game as an applet and as an application. It has the same GUI interface as the windowed version: a large canvas with two text fields at the bottom used to report the number of boxes added to the scene, and the time. Class diagrams showing the public methods are given in Figure 3-10. A comparison with the diagrams for the windowed version in Figure 3-2 show the classes stay mainly the same. The only substantial change is to replace JFrame by JApplet at the top level. Figure 3-10. Class diagrams for the WormChase applet
The Worm class is unchanged from the windowed version. The Obstacles class now calls setBoxNumber( ) in WormChaseApplet rather than WormChase. WormPanel reports its termination statistics in a different way, but the animation loop and statistics gathering are unchanged. WormChaseApplet handles pausing, resumption, and termination by tying them to events in the applet life cycle. By comparison, WormChase utilizes Window events. The applet's web page passes the requested frame rate to it as a parameter:
The WormChaseApplet ClassFigure 3-11 shows the class diagram for WormChaseApplet with all its variables and methods. Figure 3-11. WormChaseApplet in detailThe applet's init( ) method reads the FPS value from the web page, sets up the GUI, and starts the game:
makeGUI( ) is the same as the one in the JFrame version. The call to startGame( ) replaces the use of addNotify( ) in the JPanel. The applet life-cycle methodsstart( ), stop( ), and destroy( )contain calls to WormPanel to resume, pause, and terminate the game:
A browser calls destroy( ) prior to deleting the web page (and its applet) or perhaps as the browser itself is closed. The browser will wait for the destroy( ) call to return before exiting. The WormPanel ClassThe only major change to WormPanel is how printStats( ) is called. The stopGame( ) method is modified to call finishOff( ), which calls printStats( ):
finishOff( ) checks a global finishedOff Boolean to decide whether to report the statistics. finishedOff starts with the value false. finishOff( ) is called at the end of run( ) as the animation loop finishes. The first call to finishOff( ) will pass the if test, set the finishedOff flag to true, and print the data. The flag will then prevent a second call from repeating the output.
In the windowed application, stopGame( ) only sets running to false before returning, with no call to finishOff( ). The threaded animation loop may then execute for a short time before checking the flag, stopping, and calling printStats( ). This approach is fine in an application where the animation thread will be allowed to finish before the application terminates. Unfortunately, as soon as an applet's destroy( ) method returns, then the applet or the browser can exit. In this case, the animation thread may not have time to reach its printStats( ) call. To ensure the statistics are printed, finishOff( ) is called in the applet's stopGame( ) method. The other call to finishOff( ) at the end of run( ) is a catch-all in case I modify the game so it can terminate the animation loop without passing through stopGame( ). Timing ResultsThe timing results are given in Table 3-3.
The poor showing for the frame rate on the Windows 2000 machine is expected, but the applet performs well on more modern hardware. |
Friday, January 8, 2010
WormChase as an Applet
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment