O'Reilly logo

Killer Game Programming in Java by Andrew Davison

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Applying Image Effects

ImagesTests uses a Swing timer to animate its image effects rather than the active rendering approach developed in early chapters. This is purely a matter of preventing the code from becoming overly complicated since the high accuracy offered by active rendering isn't required. The visual effects employed here are generally composed from 5 to 10 distinct frames, displayed over the course of one or two seconds; this implies a need for a maximum of 10 FPS, which is within the capabilities of the Swing timer.

Tip

If necessary, the effects techniques can be easily translated to an active rendering setting.

The timer-driven framework is illustrated by Figure 6-4. The details of actionPerformed() and paintComponent() are explained below.

ImagesTests and the Swing timer

Figure 6-4. ImagesTests and the Swing timer

ImagesTests maintains a global variable (counter) that starts at 0 and is incremented at the end of each paintComponent() call, modulo 100.

Tip

The modulo operation isn't significant but is used to keep the counter value from becoming excessively large.

counter is used in many places in the code, often to generate input arguments to the visual effects.

Starting ImagesTests

The main() method for ImagesTests creates a JFrame and adds the ImagesTests JPanel to it:

 public static void main(String args[]) { // switch on translucency acceleration in Windows System.setProperty("sun.java2d.translaccel", ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required