I assume that by “it”, you mean the relevant code. The main gist of the code is as follows. To create a new cursor, use:
/** Creates a Cursor from an Image.
@param cursorName the name of the Cursor to create
@param cursorImage the Image to create the Cursor from
@param xHotSpot the x-coordinate of the cursor's hot spot
@param yHotSpot the y-coordinate of the cursor's hot spot
@return a Cursor showing [cursorImage]*/
private static Cursor createCursor(final String cursorName,
final BufferedImage cursorImage, final int xHotSpot, final int yHotSpot)
{
ImageUtil.addAlphaChannelTransparency(cursorImage);
return Toolkit.getDefaultToolkit().createCustomCursor(
cursorImage, new Point(xHotSpot, yHotSpot), cursorName);
} //end createCursor
The createCustomCursor is the important method. I had to use different hotspots for different cursors so that certain cursors would be visible at the edge of the screen.
To change which cursor is being displayed, just use Component.setCursor.
What follows is a larger portion of the code (with …'s to denote omitted code as it’s a large file) to demonstrate a little bit about how I was using the cursors. This may be useful to you, but it probably won’t be. The code is long enough to be hard to understand, and the missing sections make it even more confusing. But if you think it’s worthwhile, go ahead and take a look.
/* Note: MainDisplayViewPane extends ViewPane extends JPanel, so GameViewPane is a Component. This is why it can call setCursor.*/
public class GameViewPane extends MainDisplayViewPane
...
{
...
/** Updates this GameViewPane.*/
public void update() {
...
//set the cursor
//if the user is scrolling too far, output the "no scrolling" cursor
if(noScrollCursor != null)
setCursor(noScrollCursor);
else //else the mouse cursor should be normal, so use the selection mode's cursor
setCursor(mouseSelectionMode.getCursor());
} //end update
...
/** Creates a Cursor from an Image.
@param cursorName the name of the Cursor to create
@param cursorImage the Image to create the Cursor from
@return a Cursor showing [cursorImage]*/
private static Cursor createCursor(final String cursorName,
final BufferedImage cursorImage)
{
return createCursor(cursorName, cursorImage, 0, 0);
} //end createCursor
/** Creates a Cursor from an Image.
@param cursorName the name of the Cursor to create
@param cursorImage the Image to create the Cursor from
@param xHotSpot the x-coordinate of the cursor's hot spot
@param yHotSpot the y-coordinate of the cursor's hot spot
@return a Cursor showing [cursorImage]*/
private static Cursor createCursor(final String cursorName,
final BufferedImage cursorImage, final int xHotSpot, final int yHotSpot)
{
ImageUtil.addAlphaChannelTransparency(cursorImage);
return Toolkit.getDefaultToolkit().createCustomCursor(
cursorImage, new Point(xHotSpot, yHotSpot), cursorName);
} //end createCursor
/** Creates all the "no scroll" cursors.
@param baseDiagonalImageName the base name of the diagonal image
@param baseNondiagonalImageName the base name of the nondiagonal image
@return a 2D array of all the "no scroll" cursors*/
private static Cursor[][] createNoScrollCursors(final String baseDiagonalImageName,
final String baseNondiagonalImageName)
{
//create the array
int numPaneScrollMagnitudes = PaneScrollDirection.values().length;
Cursor[][] aaNoScrollCursor =
new Cursor[numPaneScrollMagnitudes][numPaneScrollMagnitudes];
//indices of the PaneScrollDirections
int iNegative = PaneScrollDirection.NEGATIVE.ordinal();
int iNone = PaneScrollDirection.NONE.ordinal();
int iPositive = PaneScrollDirection.POSITIVE.ordinal();
int hotSpotOffset = 8;
BufferedImage upLeftImage = loadCursorImage(baseDiagonalImageName);
BufferedImage upRightImage = ImageUtil.flipImageHorizontally(upLeftImage);
BufferedImage downLeftImage = ImageUtil.flipImageVertically(upLeftImage);
BufferedImage downRightImage = ImageUtil.flipImageVertically(upRightImage);
aaNoScrollCursor[iNegative][iNegative] =
createCursor("up-left no scroll", upLeftImage);
aaNoScrollCursor[iPositive][iNegative] =
createCursor("up-right no scroll", upRightImage,
upRightImage.getWidth() + hotSpotOffset, 0);
aaNoScrollCursor[iNegative][iPositive] =
createCursor("down-left no scroll", downLeftImage, 0,
downLeftImage.getHeight() + hotSpotOffset);
aaNoScrollCursor[iPositive][iPositive] =
createCursor("down-right no scroll", downRightImage,
downRightImage.getWidth() + hotSpotOffset,
downRightImage.getHeight() + hotSpotOffset);
BufferedImage leftImage = loadCursorImage(baseNondiagonalImageName);
BufferedImage upImage = ImageUtil.rotate90Degrees(leftImage);
BufferedImage rightImage = ImageUtil.rotate90Degrees(upImage);
BufferedImage downImage = ImageUtil.rotate90Degrees(rightImage);
aaNoScrollCursor[iNegative][iNone] = createCursor("left no scroll", leftImage);
aaNoScrollCursor[iNone][iNegative] = createCursor("up no scroll", upImage);
aaNoScrollCursor[iPositive][iNone] = createCursor("right no scroll", rightImage,
rightImage.getWidth() + hotSpotOffset, 0);
aaNoScrollCursor[iNone][iPositive] = createCursor("down no scroll", downImage,
0, downImage.getHeight() + hotSpotOffset);
return aaNoScrollCursor;
} //end createNoScrollCursors
/** Loads a cursor from a file in the cursor art directory.
@param cursorName the name of the cusor to load
@return Cursor [cursorName] or a default cursor if [cursorName] has no image file*/
private static Cursor loadCursor(final String cursorName) {
Cursor cursor = null;
BufferedImage cursorImage = loadCursorImage(cursorName);
if(cursorImage != null) {
cursor = createCursor(cursorName, cursorImage);
} else {
ErrorLog.output(null,
RpgEngineErrorList.gameViewPaneFailedToCreateCustomCursor, cursorName);
cursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
}
return cursor;
} //end loadCursor
/** Loads an image from a file in the cursor art directory.
@param cursorName the name of the cusor to load
@return the image for Cursor [cursorName] or a null if [cursorName] has no image file
*/
private static BufferedImage loadCursorImage(final String cursorName) {
BufferedImage cursorImage = null;
try {
cursorImage = ImageUtil.loadImage(
World.cursorDirectory + cursorName + World.defaultImageFilenameExtension);
} catch(Exception exception) {
ErrorLog.output(null,
RpgEngineErrorList.gameViewPaneFailedToCreateCustomCursor, cursorName);
}
return cursorImage;
} //end loadCursor
...
/** <p>Specifies how mouse selection is taking placing
@author Steven Fletcher
@since 2005/09/05
@version 2006/05/09*/
private enum MouseSelectionMode {
ATTACK("attackcursor", true),
CHOOSE_ITEM_TARGET("itemcursor", false),
CHOOSE_SPELL_TARGET("spellcursor", false),
NORMAL("normalcursor", true);
/** Constructor.
@param cursor the Cursor for this MouseSelectionMode
@param bIgnoringAlliesIsAllowed whether allies can be ignored in this
MouseSelectionMode*/
private MouseSelectionMode(final Cursor cursor,
final boolean bIgnoringAlliesIsAllowed)
{
this.cursor = cursor;
this.bIgnoringAlliesIsAllowed = bIgnoringAlliesIsAllowed;
} //end constructor
/** Constructor.
@param cursorName the name of the Cursor for this MouseSelectionMode
@param bIgnoringAlliesIsAllowed whether allies can be ignored in this
MouseSelectionMode*/
private MouseSelectionMode(final String cursorName,
final boolean bIgnoringAlliesIsAllowed)
{
this(loadCursor(cursorName), bIgnoringAlliesIsAllowed);
} //end constructor
/** gets the cursor for this MouseSelectionMode
@return the cursor for this MouseSelectionMode*/
private Cursor getCursor() {
return cursor;
} //end getCursor
/** Determines whether allies should be ignored during combat for this
MouseSelectionMode.
@return whether allies should be ignored during combat*/
private boolean isIgnoringAlliesAllowed() {return bIgnoringAlliesIsAllowed;}
//VARIABLES
private final Cursor cursor;
private final boolean bIgnoringAlliesIsAllowed;
}; //end enum MouseSelectionMode
...
} //end class GameViewPane