tags:

views:

117

answers:

2

When I'm create custom AWT cursors, it seems that they get the wrong hotspot when running on Windows Vista or Windows 7 -- the hotspot is offset by a few pixels to the right and down. On Windows XP and on Linux with X.org, I'm not seeing the problem at all. Is this a bug, or am I just doing something weird?

I'm creating the cursors with this function:

private static Cursor makeawtcurs(BufferedImage img, Coord hs) {
    java.awt.Dimension cd = Toolkit.getDefaultToolkit().getBestCursorSize(img.getWidth(), img.getHeight());
    BufferedImage buf = TexI.mkbuf(new Coord((int)cd.getWidth(), (int)cd.getHeight()));
    java.awt.Graphics g = buf.getGraphics();
    g.drawImage(img, 0, 0, null);
    g.dispose();
    return(Toolkit.getDefaultToolkit().createCustomCursor(buf, new java.awt.Point(hs.x, hs.y), ""));
}
A: 

try the class above:

to use this class do like this: Pins name = new Pins( Color.YELLOW );

and then to get a cursor: Cursor curorName = name.pinsCursor;

...you can get also an Image and an icon form this class. good luck.

/************************************************************
* Esta Classe cria os pins para assinalar os locais no mapa *
*                                       Por Paulo Franco©   *
************************************************************/

import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;

public class Pins
{
    Image pins = null;
    ImageIcon pinsII = null;
    Cursor pinsCursor;

    public Pins( Color cor )
    {
     BufferedImage pinsBI = new BufferedImage( 20, 20, BufferedImage.TYPE_INT_ARGB );
     Graphics2D pinsBIg2D = ( Graphics2D ) pinsBI.getGraphics( );

     Line2D.Double linha = new Line2D.Double( 3.0, 1.0, 3.0, 19.0 );
     pinsBIg2D.setColor( Color.BLACK );
     pinsBIg2D.setStroke( new BasicStroke( 3.0f ) );
     pinsBIg2D.draw( linha );

     GeneralPath seta = new GeneralPath( );
     seta.moveTo(  3, 1 );
     seta.lineTo( 17, 5 );
     seta.lineTo(  3, 9 );
     pinsBIg2D.setColor( cor );
     pinsBIg2D.fill( seta );

     pins = Toolkit.getDefaultToolkit( ).createImage( pinsBI.getSource( ) );
     pinsII = new ImageIcon( pins );
     pinsCursor = Toolkit.getDefaultToolkit( ).createCustomCursor( pinsBI, new Point( 10, 10 ), "" );
    }
}
how does this answer the question?
akf
A: 

Windows cursors must always be 32x32 pixels in size. It's resizing your cursor to 32x32, yet it doesn't resize the hotspot. To fix this problem, add transparent pixels to the bottom and right sides of your cursor image to make it 32x32 pixels in size.

Erick Robertson