get Closed Hand Cursor - Java java.awt

Java examples for java.awt:Cursor

Description

get Closed Hand Cursor

Demo Code


import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Main{
    protected static Cursor closedHandCursor;
    public static Cursor getClosedHandCursor() {
        if (closedHandCursor == null) {
            try {
                BufferedImage image = ImageIO
                        .read(CursorUtils.class
                                .getResourceAsStream("/m/resources/hand_closed.png"));
                Toolkit toolkit = Toolkit.getDefaultToolkit();
                Dimension dim = toolkit.getBestCursorSize(image.getWidth(),
                        image.getHeight());
                if (dim.width != image.getWidth()
                        || dim.height != image.getHeight()) {
                    BufferedImage newimage = new BufferedImage(dim.width,
                            dim.height, BufferedImage.TYPE_4BYTE_ABGR);
                    Graphics2D g2 = (Graphics2D) newimage.getGraphics();
                    g2.drawImage(image, 0, 0, new Color(255, 0, 0, 0), null);
                    image = newimage;//from  w  ww.  ja v a  2 s. c  o  m
                }
                closedHandCursor = Toolkit.getDefaultToolkit()
                        .createCustomCursor(image, new Point(15, 15),
                                "ClosedHandCursor");
            } catch (IOException ignored) {
            }
        }
        return closedHandCursor;
    }
}

Related Tutorials