Chapter 2. An Animation Framework


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).

I distinguish between windowed and full-screen applications because J2SE 1.4 introduced full-screen exclusive mode(FSEM). It suspends the normal windowing environment and allows an application to access the underlying graphics hardware more directly. FSEM permits techniques such as page flipping and provides control over the screen's resolution and image depth. The principal aim of FSEM is to accelerate graphics-intensive applications, such as games.


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:

  • The {update, render, sleep} animation loop

  • Starting and terminating an animation

  • Double buffering

  • User interaction

  • Active rendering

  • Animation control based on a user's requested FPS

  • The management of inaccuracies in the timer and sleep operations

  • Combining FPS and game state updates per second (UPS)

  • Game pausing and resumption

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.

The example programs used in this chapter can be found in the Timings/ directory. All the code directories mentioned in the chapters can be downloaded from the book's web site at http://fivedots.coe.psu.ac.th/tildad/jg.


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.



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net