Home > Articles > Programming > User Interface (UI)

Introduce Animated Cursors to Java GUIs, Part 3

  • Print
  • + Share This
Jeff Friesen completes his three-part series on a Java-based animated cursor library that extracts cursor images and other data from Windows-based .ani files by presenting his final implementation of the library. This implementation is all about aesthetics. Specifically, it focuses on supporting translucency so that animated cursors look as nice as possible.

Editor's Note: Read Part 1 and Part 2 to get up to speed on this series.

Like this article? We recommend

Because the java.awt.Cursor class does not support animated cursors, I’ve developed a Java library that lets you assign Windows .ani file-based animated cursors to arbitrary Swing components. Articles one and two in this three-part series presented basic and improved implementations of this library. This article reveals the final implementation, which focuses on supporting translucency.

Quest for Translucency

Translucency is the property by which background and foreground color information mix so that you partly see the background and partly see the foreground. In contrast, transparency allows you to see the background without seeing the foreground, and opaqueness allows you to see the foreground without seeing the background.

When confronted with an image containing an alpha channel (the bits identifying a pixel’s transparency, opaqueness, or translucency), Java’s Cursor class properly handles the channel’s transparent and opaque alpha values. However, Cursor handles translucent alpha values as if they were opaque. This treatment results in cursor images that look terrible when displayed, as evidenced in Figure 1.

Figure 1

Figure 1 Cursors look terrible when their translucency values are ignored.

Figure 1 shows one frame in the animation sequence stored in aero_working.ani, which happens to be one of the animated cursors introduced by Windows Vista. Normally, you would not see this image when running Part 1’s AniCursorDemo application with Part 1’s or Part 2’s library implementation. However, I commented out the following code in Part 2’s AniCursor.java source code to achieve Figure 1:

if (ncolors == 0)
  for (int i = 0; i < bi.getHeight (); i++)
     int [] rgb = bi.getRGB (0, i, bi.getWidth (), 1,
                 null, 0,
                 bi.getWidth ()*4);
     for (int j = 0; j < rgb.length; j++)
       int alpha = (rgb [j] >> 24) & 255;
       if (alpha < 0x80)
         alpha = 0;
         alpha = 255;
       rgb [j] &= 0x00ffffff;
       rgb [j] = (alpha << 24) | rgb [j];
     bi.setRGB (0, i, bi.getWidth (), 1, rgb, 0,
          bi.getWidth ()*4);

This code fragment, which only executes if an image has an alpha channel (the value of ncolors is 0), is responsible for converting (via a simple binary decision) the channel’s translucency values to transparent (0) or opaque (255) values. This conversion improves the appearance of the cursor image, as Figure 2 reveals.

Figure 2

Figure 2 Cursors look better when their translucency values are converted to opaque or transparent.

The cursor image’s aesthetics still leave something to be desired because the antialiasing (blending pixel colors along non-horizontal/non-vertical edges to avoid a jagged stair-step effect) information provided by the translucency values is gone. Fortunately, it’s possible to retain these values by avoiding the conversion and still overcome Figure 1’s awful aesthetics. Figure 3 proves this possibility.

Figure 3

Figure 3 Cursors look best when all of their translucency values are employed.

  • + Share This
  • 🔖 Save To Your Account