FileDocCategorySizeDatePackage
Cursor.javaAPI DocJava SE 6 API12290Tue Jun 10 00:25:14 BST 2008java.awt

Cursor

public class Cursor extends Object implements Serializable
A class to encapsulate the bitmap representation of the mouse cursor.
see
Component#setCursor
version
1.46, 08/23/07
author
Amy Fowler

Fields Summary
public static final int
DEFAULT_CURSOR
The default cursor type (gets set if no cursor is defined).
public static final int
CROSSHAIR_CURSOR
The crosshair cursor type.
public static final int
TEXT_CURSOR
The text cursor type.
public static final int
WAIT_CURSOR
The wait cursor type.
public static final int
SW_RESIZE_CURSOR
The south-west-resize cursor type.
public static final int
SE_RESIZE_CURSOR
The south-east-resize cursor type.
public static final int
NW_RESIZE_CURSOR
The north-west-resize cursor type.
public static final int
NE_RESIZE_CURSOR
The north-east-resize cursor type.
public static final int
N_RESIZE_CURSOR
The north-resize cursor type.
public static final int
S_RESIZE_CURSOR
The south-resize cursor type.
public static final int
W_RESIZE_CURSOR
The west-resize cursor type.
public static final int
E_RESIZE_CURSOR
The east-resize cursor type.
public static final int
HAND_CURSOR
The hand cursor type.
public static final int
MOVE_CURSOR
The move cursor type.
protected static Cursor[]
predefined
static final String[]
cursorProperties
int
type
The chosen cursor type initially set to the DEFAULT_CURSOR.
public static final int
CUSTOM_CURSOR
The type associated with all custom cursors.
private static final Hashtable
systemCustomCursors
private static final String
systemCustomCursorDirPrefix
private static final String
systemCustomCursorPropertiesFile
private static Properties
systemCustomCursorProperties
private static final String
CursorDotPrefix
private static final String
DotFileSuffix
private static final String
DotHotspotSuffix
private static final String
DotNameSuffix
private static final long
serialVersionUID
private static final sun.awt.DebugHelper
dbg
private transient long
pData
Hook into native data.
private transient Object
anchor
transient CursorDisposer
disposer
protected String
name
The user-visible name of the cursor.
Constructors Summary
public Cursor(int type)
Creates a new cursor object with the specified type.

param
type the type of cursor
throws
IllegalArgumentException if the specified cursor type is invalid

	if (type < Cursor.DEFAULT_CURSOR || type > Cursor.MOVE_CURSOR) {
	    throw new IllegalArgumentException("illegal cursor type");
	}
	this.type = type;

        // Lookup localized name.
        name = Toolkit.getProperty(cursorProperties[type][0],
                                   cursorProperties[type][1]);
    
protected Cursor(String name)
Creates a new custom cursor object with the specified name.

Note: this constructor should only be used by AWT implementations as part of their support for custom cursors. Applications should use Toolkit.createCustomCursor().

param
name the user-visible name of the cursor.
see
java.awt.Toolkit#createCustomCursor

        this.type = Cursor.CUSTOM_CURSOR;
        this.name = name;
    
Methods Summary
private static native voidfinalizeImpl(long pData)

public static java.awt.CursorgetDefaultCursor()
Return the system default cursor.

        return getPredefinedCursor(Cursor.DEFAULT_CURSOR);
    
public java.lang.StringgetName()
Returns the name of this cursor.

return
a localized description of this cursor.
since
1.2

	return name;
    
public static java.awt.CursorgetPredefinedCursor(int type)
Returns a cursor object with the specified predefined type.

param
type the type of predefined cursor
return
the specified predefined cursor
throws
IllegalArgumentException if the specified cursor type is invalid

	if (type < Cursor.DEFAULT_CURSOR || type > Cursor.MOVE_CURSOR) {
	    throw new IllegalArgumentException("illegal cursor type");
	}
	if (predefined[type] == null) {
	    predefined[type] = new Cursor(type);
	}
	return predefined[type];
    
public static java.awt.CursorgetSystemCustomCursor(java.lang.String name)
Returns a system-specific custom cursor object matching the specified name. Cursor names are, for example: "Invalid.16x16"

param
name a string describing the desired system-specific custom cursor
return
the system specific custom cursor named
exception
HeadlessException if GraphicsEnvironment.isHeadless returns true

        GraphicsEnvironment.checkHeadless();
	Cursor cursor = (Cursor)systemCustomCursors.get(name);

	if (cursor == null) {
	    synchronized(systemCustomCursors) {
		if (systemCustomCursorProperties == null)
		    loadSystemCustomCursorProperties();
	    }

	    String prefix = CursorDotPrefix + name;
	    String key    = prefix + DotFileSuffix;

	    if (!systemCustomCursorProperties.containsKey(key)) {
	        if (dbg.on) {
	            dbg.println("Cursor.getSystemCustomCursor(" + name + ") returned null");
	        } 
	        return null;
	    }

	    final String fileName =
		systemCustomCursorProperties.getProperty(key);

	    String localized = (String)systemCustomCursorProperties.getProperty(prefix + DotNameSuffix);

	    if (localized == null) localized = name;

	    String hotspot = (String)systemCustomCursorProperties.getProperty(prefix + DotHotspotSuffix);

	    if (hotspot == null)
	    	throw new AWTException("no hotspot property defined for cursor: " + name);

	    StringTokenizer st = new StringTokenizer(hotspot, ",");

	    if (st.countTokens() != 2)
	    	throw new AWTException("failed to parse hotspot property for cursor: " + name);

	    int x = 0;
	    int y = 0;

	    try {
		x = Integer.parseInt(st.nextToken());
		y = Integer.parseInt(st.nextToken());
	    } catch (NumberFormatException nfe) {
	    	throw new AWTException("failed to parse hotspot property for cursor: " + name);
	    }

	    try {
		final int fx = x;
		final int fy = y;
		final String flocalized = localized;

                cursor = (Cursor) java.security.AccessController.doPrivileged(
		    new java.security.PrivilegedExceptionAction() {
		    public Object run() throws Exception {
			Toolkit toolkit = Toolkit.getDefaultToolkit();
			Image image = toolkit.getImage(
			   systemCustomCursorDirPrefix + fileName);
			return toolkit.createCustomCursor(
				    image, new Point(fx,fy), flocalized);
		    }
		});
	    } catch (Exception e) {
		throw new AWTException(
                    "Exception: " + e.getClass() + " " + e.getMessage() +
                    " occurred while creating cursor " + name);
	    }

	    if (cursor == null) {
	        if (dbg.on) {
	            dbg.println("Cursor.getSystemCustomCursor(" + name + ") returned null");
		} 
	    } else {
	        systemCustomCursors.put(name, cursor);
	    }
	}

	return cursor;
    
public intgetType()
Returns the type for this cursor.

	return type;
    
private static java.lang.StringinitCursorDir()


        
	String jhome =	(String) java.security.AccessController.doPrivileged(
               new sun.security.action.GetPropertyAction("java.home"));
	return jhome +
	    File.separator + "lib" + File.separator + "images" +
	    File.separator + "cursors" + File.separator;
    
private static native voidinitIDs()
Initialize JNI field and method IDs for fields that may be accessed from C.

private static voidloadSystemCustomCursorProperties()

	synchronized(systemCustomCursors) {
	    systemCustomCursorProperties = new Properties();

            try {
	    	AccessController.doPrivileged(
                      new java.security.PrivilegedExceptionAction() {
		    public Object run() throws Exception {
			FileInputStream fis = null;
			try {
			    fis = new FileInputStream(
					   systemCustomCursorPropertiesFile);
			    systemCustomCursorProperties.load(fis);
			} finally {
			    if (fis != null)
				fis.close();
			}
			return null;
		    }
		});
	    } catch (Exception e) {
		systemCustomCursorProperties = null;
		 throw new AWTException("Exception: " + e.getClass() + " " +
		   e.getMessage() + " occurred while loading: " +
					systemCustomCursorPropertiesFile);
	    }
    	}
    
private voidsetPData(long pData)

        this.pData = pData;
        if (GraphicsEnvironment.isHeadless()) {
            return;
        }
        if (disposer == null) {
            disposer = new CursorDisposer(pData);
            // anchor is null after deserialization
            if (anchor == null) {
                anchor = new Object();
            }
            sun.java2d.Disposer.addRecord(anchor, disposer);
        } else {
            disposer.pData = pData;
        }
    
public java.lang.StringtoString()
Returns a string representation of this cursor.

return
a string representation of this cursor.
since
1.2

	return getClass().getName() + "[" + getName() + "]";