Cursor Customization

Cursor Customization

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 availablecursors. Is this possible?

JDK 1.0.2 allowed you to change the cursor of a Frame to one of a setof predefined cursors. JDK 1.1 added the ability to set the cursorfor any component with the setCursor(Cursor) method injava.awt.Component. But you could still only choose from a predefinedset of cursors. JDK 1.2 finally added the ability to create your owncustom cursors from arbitrary images.

The Tookit class contains a method with the signaturecreateCustomCursor(Image cursor, Point hotspot, Stringname). The first argument is the custom cursor image, thesecond argument is a point indicating which part of the image is thecursor hotspot, and the third argument is a textual description ofthe cursor intended for use by the Java Accessibility API. Before youcreate an Image for your custom cursor, you need to determine asupported size. Toolkit.getBestCursorSize(int, int) will return thesupported dimension closest to your preferred size. If theimplementation does not support custom cursors, a dimension of 0 widthand height will be returned.

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

import java.awt.*;import java.awt.event.*;import java.awt.image.*;public class CustomCursor {  public static final int PREFERRED_CURSOR_WIDTH  = 16;  public static final int PREFERRED_CURSOR_HEIGHT = 16;  public static void main(String[] args) {    Frame frame = new Frame("Custom Cursor");    TextArea textArea;    WindowListener exitListener;    Dimension cursorSize;    Toolkit toolkit;    BufferedImage cursorImage;    Graphics2D graphics;    Cursor cursor;    exitListener = new WindowAdapter() {      public void windowClosing(WindowEvent e) {        Window window = e.getWindow();        window.setVisible(false);        window.dispose();        System.exit(0);      }    };    frame.addWindowListener(exitListener);    textArea = new TextArea(40, 10);    textArea.setEditable(false);    frame.add(textArea, BorderLayout.CENTER);    frame.setSize(400, 400);    frame.setVisible(true);    textArea.append("Preferred Width : " + PREFERRED_CURSOR_WIDTH + "
");    textArea.append("Preferred Height: " + PREFERRED_CURSOR_HEIGHT + "
");    toolkit = Toolkit.getDefaultToolkit();    cursorSize =       toolkit.getBestCursorSize(PREFERRED_CURSOR_WIDTH,				PREFERRED_CURSOR_HEIGHT);    textArea.append("Best Width : " + cursorSize.width + "
");    textArea.append("Best Height: " + cursorSize.height + "
");    if(cursorSize.width == 0 || cursorSize.height == 0) {      textArea.append("Custom Cursor not supported.
");      return;    }    cursorImage =       new BufferedImage(cursorSize.width, cursorSize.height,			BufferedImage.TYPE_INT_ARGB);    graphics = cursorImage.createGraphics();    // Draw a rectangle of transparent pixels    graphics.setPaint(new Color(0, 0, 0, 0));    graphics.fillRect(0, 0, cursorSize.width, cursorSize.height);    // Make a Pacman-shaped cursor    graphics.setPaint(;    graphics.fillArc(0, 0, cursorSize.width, cursorSize.height,		     150, -300);    cursor =      toolkit.createCustomCursor(cursorImage,				 new Point(cursorSize.width/2,					   cursorSize.height/2),				 "Demo Cursor");    textArea.setCursor(cursor);  }}


Share the Post: