A core technology for a good game is an animation algorithm that produces reliably fast game play across various OSes (e.g. flavours of Windows, Linux, the Macintosh), and in different kinds of Java programs (e.g. applets, windowed and full-screen applications).
We distinguish between windowed and full-screen applications since J2SE v1.4 introduced full-screen exclusive mode (FSEM). It suspends the normal windowing environment, and allows an application to more directly access the underlying graphics hardware. It permits techniques such as page flipping and provides control over the screen's resolution and image depth. The principal aim of FSEM is to speed up graphics-intensive applications, such as games.
The animation algorithm developed through most of this chapter is embedded in a JPanel subclass (called GamePanel), which acts as a canvas for drawing 2D graphics (e.g. lines, circles, text, images). The animation is managed by a thread which ensures that it progresses at a consistent rate, as independent of the vagaries of the hardware and OS as possible. The rate is measured in terms of frames per second (FPS), where a frame corresponds to a single rendering of the application (game) to the canvas.
GamePanel is gradually refined and expanded through the chapter, introducing notions such as:
We also examine two other ways of coding animation, using the Swing timer and the 'utility' timer in java.util timer. The utility timer test code is illustrated in the diagram above.
In chapters 3 and 4, we develop applet, windowed and full-screen applications for a WormChase game using the final version of GamePanel (with minor variations). As a side-effect of the game play, statistics are gathered, including the average FPS and UPS, to show that GamePanel supports consistently high-speed animation.