Change Icon of cursor in Java Desktop Application
Default cursors supported by Java Desktop Application are listed below -
| Modifier and Type | Field and Description |
|---|---|
static int | CROSSHAIR_CURSOR
The crosshair cursor type.
|
static int | CUSTOM_CURSOR
The type associated with all custom cursors.
|
static int | DEFAULT_CURSOR
The default cursor type (gets set if no cursor is defined).
|
static int | E_RESIZE_CURSOR
The east-resize cursor type.
|
static int | HAND_CURSOR
The hand cursor type.
|
static int | MOVE_CURSOR
The move cursor type.
|
static int | N_RESIZE_CURSOR
The north-resize cursor type.
|
protected String | name
The user-visible name of the cursor.
|
static int | NE_RESIZE_CURSOR
The north-east-resize cursor type.
|
static int | NW_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 int | S_RESIZE_CURSOR
The south-resize cursor type.
|
static int | SE_RESIZE_CURSOR
The south-east-resize cursor type.
|
static int | SW_RESIZE_CURSOR
The south-west-resize cursor type.
|
static int | TEXT_CURSOR
The text cursor type.
|
static int | W_RESIZE_CURSOR
The west-resize cursor type.
|
static int | WAIT_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
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