14.4. Optimizations

Performance tuning is a complex subject — I've really only scratched the surface in this chapter. The example programs show the relative "cost" of different kinds of operations, but there are several techniques you can use to optimize your applications. Two of these techniques are listed below:

prerendering

For some applications it makes sense to render transformed shapes, images, or text into offscreen buffers. These can be selectively transferred to the screen during an animation. In many cases, this technique is significantly faster than doing the rendering in real time. It's a trade-off — you buy speed by using more memory.

smart update

The examples in this chapter erase the offscreen buffer and render the whole thing at every time step. In some applications, it's possible to redraw just a portion of the entire onscreen component. For example, this technique would be helpful in animating a small character over a large background.

Other optimization techniques exist, but they are out of the scope of this book. This chapter gives you the tools you need to examine the cost of different operations in the 2D API.

Get Java 2D Graphics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.