public static Cursor createImageCursor ( Image img , Point hotspot ) { Toolkit tk = Toolkit . getDefaultToolkit ( ) ; // for now, just report the cursor restrictions, then blindly create int w = img . getWidth ( null ) ; int h = img . getHeight ( null ) ; Dimension d = tk . getBestCursorSize ( w , h ) ; // int colors = tk.getMaximumCursorColors(); // Log.debug("Creating custom cursor [desiredSize=" + w + "x" + h + // ", bestSize=" + d.width + "x" + d.height + // ", maxcolors=" + colors + "]."); // if the passed-in image is smaller, pad it with transparent pixels and use it anyway. if ( ( ( w < d . width ) && ( h <= d . height ) ) || ( ( w <= d . width ) && ( h < d . height ) ) ) { Image padder = GraphicsEnvironment . getLocalGraphicsEnvironment ( ) . getDefaultScreenDevice ( ) . getDefaultConfiguration ( ) . createCompatibleImage ( d . width , d . height , Transparency . BITMASK ) ; Graphics g = padder . getGraphics ( ) ; g . drawImage ( img , 0 , 0 , null ) ; g . dispose ( ) ; // and reassign the image to the padded image img = padder ; // and adjust the 'best' to cheat the hotspot checking code d . width = w ; d . height = h ; } // make sure the hotspot is valid if ( hotspot == null ) { hotspot = new Point ( d . width / 2 , d . height / 2 ) ; } else { hotspot . x = Math . min ( d . width - 1 , Math . max ( 0 , hotspot . x ) ) ; hotspot . y = Math . min ( d . height - 1 , Math . max ( 0 , hotspot . y ) ) ; } // and create the cursor return tk . createCustomCursor ( img , hotspot , "samskivertDnDCursor" ) ; }
Create a custom cursor out of the specified image with the specified hotspot .