/**
 * CursorUtil.java
 *
 * @author Created by Omnicore CodeGuide
 */

package edu.sc.seis.gee.task;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.net.URL;



public class CursorUtil {

    public static Cursor getCursor(String name, String imageFilePath, float hotPointX, float hotPointY) {
        Toolkit tools = Toolkit.getDefaultToolkit();
        Dimension dim = tools.getBestCursorSize(16, 16);
        int lastDot = imageFilePath.lastIndexOf(".");
        String sizeImageFilePath = imageFilePath.substring(0, lastDot);
        if (dim.getWidth() == 16) {
            sizeImageFilePath += 16 + imageFilePath.substring(lastDot);
        } else {
            sizeImageFilePath += 32 + imageFilePath.substring(lastDot);
        }
        Point hotspot = new Point((int)(hotPointX*dim.getWidth()),(int)(hotPointY*dim.getHeight()));

        URL imgURL = CursorUtil.class.getClassLoader().getResource(sizeImageFilePath);
        if (imgURL == null) {
            imgURL = CursorUtil.class.getClassLoader().getResource(imageFilePath);
        }
        Image image = tools.createImage(imgURL);
        Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(image, hotspot, name);
        if (cursor == null) {
            cursor = Cursor.getDefaultCursor();
        }
        return cursor;
    }
}


