Java tutorial
/* * @(#)CursorUtil.java 1.0 Apr 27, 2008 * * The MIT License * * Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ //package org.eoti.awt; import java.awt.*; import java.awt.image.*; public class CursorUtil { public enum CursorType { Crosshair(Cursor.CROSSHAIR_CURSOR), //Custom(Cursor.CUSTOM_CURSOR), Default(Cursor.DEFAULT_CURSOR), EastResize(Cursor.E_RESIZE_CURSOR), Hand(Cursor.HAND_CURSOR), Move( Cursor.MOVE_CURSOR), None(createNoCursor()), NorthResize(Cursor.N_RESIZE_CURSOR), NorthEastResize( Cursor.NE_RESIZE_CURSOR), NorthWestResize(Cursor.NW_RESIZE_CURSOR), SouthResize( Cursor.S_RESIZE_CURSOR), SouthEastResize(Cursor.SE_RESIZE_CURSOR), SouthWestResize( Cursor.SW_RESIZE_CURSOR), Text(Cursor.TEXT_CURSOR), WestResize( Cursor.W_RESIZE_CURSOR), Wait(Cursor.WAIT_CURSOR); CursorType(int type) { cursor = new Cursor(type); } CursorType(Cursor cursor) { this.cursor = cursor; } protected Cursor cursor; public Cursor getCursor() { return cursor; } } protected static Cursor createNoCursor() { try { return createCursor(GraphicsUtil.createImage(1, 1), 0, 0, "NO_CURSOR"); } catch (Exception e) { return null; } } public static Cursor createCursor(BufferedImage img, int hotspotX, int hotspotY, String name) throws IndexOutOfBoundsException, Exception { return createCursor(img, new Point(hotspotX, hotspotY), name); } public static Cursor createCursor(BufferedImage img, Point hotspot, String name) throws IndexOutOfBoundsException, Exception { Toolkit tk = Toolkit.getDefaultToolkit(); Dimension d = tk.getBestCursorSize(img.getWidth(), img.getHeight()); if ((d.width == img.getWidth()) && (d.height == img.getHeight())) return tk.createCustomCursor(img, hotspot, name); if ((d.width + d.height) < 2) throw new Exception("Invalid Size"); BufferedImage newImg = GraphicsUtil.createImage(d.width, d.height); Graphics2D g2 = newImg.createGraphics(); g2.drawImage(img, // what to draw 0, // dest left 0, // dest top newImg.getWidth(), // dest right newImg.getHeight(), // dest bottom 0, // src left 0, // src top img.getWidth(), // src right img.getHeight(), // src bottom null // to notify of image updates ); return tk.createCustomCursor(newImg, hotspot, name); } }