Tuesday, November 18, 2008

Rendering Hints: Java Anti-aliasing

Description of Purpose:
One of the neat things that the java.awt.Graphics2D class provides is the ability to turn on anti-aliasing for drawing primitives. This makes drawing vector graphics for your game much more attractive and simple. I will demonstrate a few usages for utilitizing anti-aliasing in hard-coded primitive animations to provide various effects.

The basic idea of anti-aliasing is that you soften the edges when drawing lines, circles, arcs, ovals, or any other primitive. You can read more about it here.

The example code will build upon what DoubleBuffering example code.

Example:
Before/After



Quick And Dirty:
Enabling anti-aliasing for your Graphics2D objects is very easy. Just one function call and you've enabled it. It couldn't be simpler.

Using the setRenderingHint function in the java.awt.Graphics2D class.
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
RenderingHints.VALUE_ANTIALIAS_ON);

Now any primitive that you draw, i.e. polygons, rectangles, arcs, ovals, etc... Using that graphics instance will be anti-aliased.

Pretty simple right, there are also other rendering hints that can help increase the quality of your graphics. However, it's a good idea to use them sparingly since they do require more time to render.

The Example:
The example can pretty easily be extended to provide a means for testing different mouse over effects or just animated effects in general. If you come up with neat effects that you would like to share please feel free to post them.

No comments:

Post a Comment