Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: AWT
Expertise: Beginner
Sep 7, 1999

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.

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 + "\n");
    textArea.append("Preferred Height: " + PREFERRED_CURSOR_HEIGHT + "\n");

    toolkit = Toolkit.getDefaultToolkit();
    cursorSize = 
      toolkit.getBestCursorSize(PREFERRED_CURSOR_WIDTH,
				PREFERRED_CURSOR_HEIGHT);

    textArea.append("Best Width : " + cursorSize.width + "\n");
    textArea.append("Best Height: " + cursorSize.height + "\n");

    if(cursorSize.width == 0 || cursorSize.height == 0) {
      textArea.append("Custom Cursor not supported.\n");
      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(Color.black);
    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);
  }
}
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap