Example usage for java.awt Toolkit getBestCursorSize

List of usage examples for java.awt Toolkit getBestCursorSize

Introduction

In this page you can find the example usage for java.awt Toolkit getBestCursorSize.

Prototype

public Dimension getBestCursorSize(int preferredWidth, int preferredHeight) throws HeadlessException 

Source Link

Document

Returns the supported cursor dimension which is closest to the desired sizes.

Usage

From source file:Main.java

public static Cursor buildCursorByTrimming(Toolkit toolkit, Image image, int x, int y, String name,
        Cursor defaultCursor) {/*from  w w w . j  a v  a2  s.  c om*/
    Dimension d = toolkit.getBestCursorSize(image.getWidth(null), image.getHeight(null));
    if (d == null || d.getWidth() <= 0 || d.getHeight() <= 0)
        return defaultCursor;
    BufferedImage out = new BufferedImage((int) d.getWidth(), (int) d.getHeight(), BufferedImage.TYPE_INT_ARGB);
    out.getGraphics().drawImage(image, 0, 0, null);
    return toolkit.createCustomCursor(out, new Point(x, y), name);
}

From source file:CursorUtil.java

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
    );//from ww w  . java  2  s.  c om

    return tk.createCustomCursor(newImg, hotspot, name);
}

From source file:Main.java

/**
 * Create a custom cursor out of the specified image, with the specified hotspot.
 */// www  . j  a v  a  2 s.c o  m
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");
}

From source file:org.dishevelled.brainstorm.BrainStorm.java

/**
 * Create and return a new hidden cursor, that is a fully transparent one pixel custom cursor.
 *
 * @return a new hidden cursor/*from  ww  w.jav  a2  s . c om*/
 */
private Cursor createHiddenCursor() {
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension size = toolkit.getBestCursorSize(1, 1);
    BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = image.createGraphics();
    graphics.setBackground(new Color(0.0f, 0.0f, 0.0f, 0.0f));
    graphics.clearRect(0, 0, size.width, size.height);
    graphics.dispose();
    return toolkit.createCustomCursor(image, new Point(0, 0), "hiddenCursor");
}