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

Answer:
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(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);  }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may