Pages

Change Icon of cursor in Java Desktop Application

Thursday, 17 October 2013

 Change Icon of cursor in Java Desktop Application 


Default cursors supported by Java Desktop Application are listed  below -

 CURSOR TYPES  
Modifier and TypeField and Description
static intCROSSHAIR_CURSOR
The crosshair cursor type.
static intCUSTOM_CURSOR
The type associated with all custom cursors.
static intDEFAULT_CURSOR
The default cursor type (gets set if no cursor is defined).
static intE_RESIZE_CURSOR
The east-resize cursor type.
static intHAND_CURSOR
The hand cursor type.
static intMOVE_CURSOR
The move cursor type.
static intN_RESIZE_CURSOR
The north-resize cursor type.
protected Stringname
The user-visible name of the cursor.
static intNE_RESIZE_CURSOR
The north-east-resize cursor type.
static intNW_RESIZE_CURSOR
The north-west-resize cursor type.
protected static Cursor[]predefined
Deprecated.
As of JDK version 1.7, the getPredefinedCursor(int) method should be used instead.
static intS_RESIZE_CURSOR
The south-resize cursor type.
static intSE_RESIZE_CURSOR
The south-east-resize cursor type.
static intSW_RESIZE_CURSOR
The south-west-resize cursor type.
static intTEXT_CURSOR
The text cursor type.
static intW_RESIZE_CURSOR
The west-resize cursor type.
static intWAIT_CURSOR
The wait cursor type.


If u want to add custom image of cursor use following code -

Loading the Cursor Image
The image for the custom cursor needs to be loaded into an Image object.

Image image = Toolkit.getDefaultToolkit().getImage("image.png");  // place image in root folder of ur project

Defining the Cursor Hot Spot
The cursor hot spot is used for the point location in mouse events. For a cursor such as a cross-hair cursor, the hot spot would be in the center of the cursor. In our example, the cursor hot spot will be at the pencil point or the point 0,0.

           Point hotSpot = new Point(0,0);

Creating the Custom Cursor
To create the custom image, we put together the cursor image and the hot spot:

           Cursor oneDisk =getToolkit().createCustomCursor(image, hotSpot,"string ");

Displaying the Custom Cursor
The final step is to notify the component to display the cursor.

           PaintWithOnScreenCanvas.this.setCursor(oneDisk);

No comments:

Post a Comment

 

Blogger news

Blogroll

Most Reading

Tags