Cmq avevo trovato una soluzione dimmi tu se sembra carina..
Ho realizzato una implementazione personalizzata della classe Cursor.
La classe ha dei metodi accesor che restituiscono i cursor di default
e contiene un metodo getLocalCustomCursor(final String name)
che preleva i file grafici dalla directory corrente del programma.
Quindi inserendo in una directory, chiamata "imagecuesors", tutti i file grafici
e un file cursors.properties la mia classe restituisce un oggetto Cursor
con il nome specificato dal file .properties.
Quindi ad ogni file grafico che viene inserito bisogna configurate
i dati all'interno del file .properties es:
devo inserire un immagine.gif nel properties inserisco:
Cursor.immagine.File=immagine.gif
Cursor.immagine.HotSpot=16,16
Cursor.immagine.Name=miaImmagine
il programma imposta il cursor con la seguente istruzione:
codice:
aWindow.setCursor(MyCursor.getLocalCustomCursor("immagine"));
Di seguito la mia implementazione:
codice:
package library.guiutility.cursor;
import java.awt.AWTException;
import java.awt.Cursor;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import java.security.AccessController;
import java.util.Hashtable;
import java.util.Properties;
import java.util.StringTokenizer;
/**
*
* @author mau2
*/
public class MyCursor {
public MyCursor(){
}
int type = Cursor.DEFAULT_CURSOR;
private static String cursorDir = System.getProperty("user.dir");//JavaEnv.getProperty(JavaEnv.user_dir);
public static Cursor get_CROSSHAIR_CURSOR(){
return Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
}
public static Cursor get_CUSTOM_CURSOR(){
return Cursor.getPredefinedCursor(Cursor.CUSTOM_CURSOR);
}
public static Cursor get_DEFAULT_CURSOR(){
return Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
}
public static Cursor get_E_RESIZE_CURSOR(){
return Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR);
}
public static Cursor get_HAND_CURSOR(){
return Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
}
public static Cursor get_MOVE_CURSOR(){
return Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
}
public static Cursor get_NE_RESIZE_CURSOR(){
return Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR);
}
public static Cursor get_NW_RESIZE_CURSOR(){
return Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR);
}
public static Cursor get_N_RESIZE_CURSOR(){
return Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR);
}
public static Cursor get_SE_RESIZE_CURSOR(){
return Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR);
}
public static Cursor get_SW_RESIZE_CURSOR(){
return Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR);
}
public static Cursor get_S_RESIZE_CURSOR(){
return Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR);
}
public static Cursor get_TEXT_CURSOR(){
return Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR);
}
public static Cursor get_WAIT_CURSOR(){
return Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
}
public static Cursor get_W_RESIZE_CURSOR(){
return Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR);
}
public static Cursor getDefaultCursor(){
return Cursor.getDefaultCursor();
}
public static Cursor getSystemCustomCursor(String name){
Cursor cursor = null;
try {
cursor = Cursor.getSystemCustomCursor(name);
} catch (AWTException e) {
}
return cursor;
}
/**
* Returns the name of this cursor.
* @return a localized description of this cursor.
* @since 1.2
*/
public String getName() {
return name;
}
//questa sezione definisce i cursor come il metodo
//Cursor.getSystemCustomCursor() soltanto che la
//directory dei file grafici si trova nella directory
//corrente del programma
private static final Hashtable<String, Cursor> systemCustomCursorsHashTable = new Hashtable<String, Cursor>(1);
private static final String systemCustomCursorDirPrefix = initCursorDir();
private static String initCursorDir() {
System.out.println(cursorDir);
return cursorDir +
File.separator + "imagecursors" + File.separator ;
}
private static final String systemCustomCursorPropertiesFile = systemCustomCursorDirPrefix + "cursors.properties";
private static Properties systemCustomCursorProperties = null;
private static final String CursorDotPrefix = "Cursor.";
private static final String DotFileSuffix = ".File";
private static final String DotHotspotSuffix = ".HotSpot";
private static final String DotNameSuffix = ".Name";
/*
* JDK 1.1 serialVersionUID
*/
private static final long serialVersionUID = 8028237497568985505L;
/**
* The user-visible name of the cursor.
*
* @serial
* @see #getName()
*/
protected String name;
static public Cursor getLocalCustomCursor(final String name) {
Cursor cursor = null;
try{
cursor = (Cursor)systemCustomCursorsHashTable.get(name);
if (cursor == null) {
synchronized(systemCustomCursorsHashTable) {
if (systemCustomCursorProperties == null)
loadLocalCustomCursorProperties();
}
String prefix = CursorDotPrefix + name;
String key = prefix + DotFileSuffix;
if (!systemCustomCursorProperties.containsKey(key)) {
System.err.println(" Cursor.getSystemCustomCursor(" + name + ") returned null");
return Cursor.getDefaultCursor();
}
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) {
return Cursor.getDefaultCursor();
} else {
systemCustomCursorsHashTable.put(name, cursor);
}
}
}catch (AWTException e){
e.printStackTrace(System.err);
}catch (HeadlessException e){
e.printStackTrace(System.err);
}
if (cursor == null)
return Cursor.getDefaultCursor();
else
return cursor;
}
private static void loadLocalCustomCursorProperties() throws AWTException {
synchronized(systemCustomCursorsHashTable) {
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);
}
}
}
}