A core technology for a good game is an animation algorithm that produces reliably fast game play across various operating systems (e.g., flavors of Windows, Linux, and Macintosh), and in different kinds of Java programs (e.g., applets, windowed, and full-screen applications).
The common ground between windowed and full-screen application is the game's animation algorithm, which is the subject of this chapter. The algorithm 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 to the canvas. GamePanel is gradually refined and expanded through the chapter, introducing the following notions:
Though most of this chapter is about the GamePanel animation loop, I will consider two other popular approaches to implementing animation: using the Swing timer and the utility timer in java.util.timer.
In Chapters 3 and 4, I 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. |