Cursor Customization

Question: I can set the cursor to a variety of stock cursors (e.g., HAND_CURSOR)
but would like to use my own images to extend the available
cursors. Is this possible?

Answer: JDK 1.0.2 allowed you to change the cursor of a Frame to one of a set
of predefined cursors. JDK 1.1 added the ability to set the cursor
for any component with the setCursor(Cursor) method in
java.awt.Component. But you could still only choose from a predefined
set of cursors. JDK 1.2 finally added the ability to create your own
custom cursors from arbitrary images.

The Tookit class contains a method with the signature
createCustomCursor(Image cursor, Point hotspot, String
name). The first argument is the custom cursor image, the
second argument is a point indicating which part of the image is the
cursor hotspot, and the third argument is a textual description of
the cursor intended for use by the Java Accessibility API. Before you
create an Image for your custom cursor, you need to determine a
supported size. Toolkit.getBestCursorSize(int, int) will return the
supported dimension closest to your preferred size. If the
implementation does not support custom cursors, a dimension of 0 width
and height will be returned.

The following program shows how to use the new cursor customization
methods to create a cursor that looks like Pacman and displays the
cursor dimensions.