Home > Articles > Programming > User Interface (UI)

Introduce Animated Cursors to Java GUIs, Part 3

Jeff Friesen
  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Close WindowJeff Friesen

Jeff Friesen

Learn more…

Using Transitions to Simplify JavaFX Animations
Sep 9, 2009
Styling Swing Components with Java CSS
Aug 28, 2009
Show Me the Movie with DirectShow
Apr 28, 2009
Playing Media with Java Media Components
Feb 27, 2009
Creating Java User Interfaces with Project Scene Graph
Feb 10, 2009
Blending Images in Java
Sep 12, 2008
Introduce Animated Cursors to Java GUIs, Part 3
Apr 30, 2008
Introduce Animated Cursors to Java GUIs, Part 2
Apr 18, 2008
Introduce Animated Cursors to Java GUIs, Part 1
Apr 11, 2008
Enhance Java GUIs with Windows Icons
Apr 4, 2008
Taming Mustang, Part 2: Scripting API Tour
Nov 2, 2007
Visit Java's Relatives: Jython and Groovy
May 4, 2007
Generics 101: Mastering the Fundamentals
Mar 23, 2007
Taming Mustang, Part 3: A New Script Engine
Mar 2, 2007
Taming Mustang, Part 1: Collections API
Feb 16, 2007
PCX Meets Image I/O: Creating an Image-Reading Java Plug-in
Dec 29, 2006
Mustang (Java SE 6) Gallops into Town
Oct 20, 2006
GridBagLayout Versus FormLayout
Oct 6, 2006
Laying Out Realistic GUIs the GridBagLayout Way
Sep 29, 2006
Harness the Power of Java's GridBagLayout
Sep 22, 2006
Tools of the Trade: SwingX Meets Swing with New and Extended Components
Aug 4, 2006
Have Fun with the Custom Screensavers Library
Mar 10, 2006
Build Screensavers with a Custom Screensavers Library in Borland C++
Feb 24, 2006
Tools of the Trade: Flash meets Java with Transform SWF and JFlashPlayer
Feb 17, 2006
Tools of the Trade, Part 3: Using the JGoodies Animation Library
Dec 22, 2005
Tools of the Trade, Part 2: Building Graphs with JGraph
Dec 9, 2005
Tools of the Trade, Part 1: Creating PDF documents with iText
Nov 4, 2005
From Literals to Expressions in Java
Aug 16, 2002
Build Your Own Java-Based Email Programs
May 10, 2002
Exploring Java's Network API: URIs and URLs
May 1, 2002
Exploring Java's Network API: Sockets
Apr 19, 2002
Basic Thread Operations in Java
Mar 22, 2002
Working with Streams in Java
Mar 22, 2002
Advanced Tips for More Powerful Tables
Nov 20, 2001
Exploring Swing's Table Component
Nov 20, 2001
Simple Tips for More Powerful Tables
Nov 20, 2001
A Handful of Tips for Swing Programs
Apr 13, 2001
A Trio of Tips for AWT Programs
Apr 13, 2001
Automating Programs with Robots
Apr 13, 2001
Build Your Own Media Player
Apr 13, 2001
Drawing Cubic Curves
Apr 13, 2001
Scaling Images
Apr 13, 2001
Using the Swing API Timers
Apr 13, 2001

Sorry, this author hasn't posted any blogs.

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.

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;
       else
         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 ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

Danny KalevMinutes from the October 2009 Meeting
By Danny Kalev on November 19, 2009 No Comments

The minutes from the Santa Cruz (October 2009) meeting are available here. Even if you're not a language layer at heart, I encourage you to read them.

Danny KalevA Reader's Opinion on Attributes
By Danny Kalev on October 20, 2009 No Comments

In August I dedicated a series to the debate about C++0x attributes. I believe that it covered the subject in a balanced and detailed way, but I keep getting complaints from C++ users who don't like attributes for various reasons. Here's a recent email I received from a Polish C++ programmer. While it  doesn't represent my opinion about attributes -- I'm rather neutral about this feature and consider it a "solution waiting for a problem" -- but it suggests that attributes are still a highly controversial issue that will haunt C++ for a long time. The email is quoted here with minor edits that and as usual, with all private details removed.

Danny KalevFollowup: The Web 2.0 Guy I Ain't
By Danny Kalev on October 16, 2009 1 Comment

Almost a year ago, I posted here The Web 2.0 Guy I Ain't. People wonder whether I still resist all those Web 2.0 features and technologies at the end of 2009.

See All Related Blogs

Informit Network