O'Reilly logo

Java 2D Graphics by Jonathan Knudsen

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

14.2. See for Yourself

To see the effect of 2D operations on animation speeds, let's develop an animation application framework. Through the rest of the chapter, I'll develop classes that plug in to this framework to perform animation.

The framework is composed of two classes, AnimationComponent and AnimationFrame. AnimationFrame serves as a container for Animation-Component and displays the current animation rate.

14.2.1. AnimationComponent

The AnimationComponent class is an abstract subclass of java.awt.Component. It contains one additional method, timeStep(), that is called every time the animation should be moved ahead by a single frame. Creating a subclass of AnimationComponent is as simple as defining two methods:

public void timeStep()

This method is called before every frame of the animation. Your subclass should update its internal state appropriately. For a bouncing ball animation, for example, this method might update the position of the ball.

public void paint(Graphics g)

This is the paint() method defined in Component. This method should render the component's current state. For the bouncing ball animation, this method should draw the ball in its current location.

AnimationComponent is a Runnable, which means it can be placed in its own thread. The run() method is a tight loop that renders the current frame (by calling render()) and prepares for the next frame (by calling timeStep()). It also calculates a frame rate. I'll explain how that works a little later. ...

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