Java ImageIcon createCursor(ImageIcon icon, Point hotspot)

Here you can find the source of createCursor(ImageIcon icon, Point hotspot)

Description

Creates a cursor from an ImageIcon

License

Open Source License

Parameter

Parameter Description
icon the icon for the cursor
hotspot the cursor's hotspot

Return

the newly created cursor

Declaration

public static Cursor createCursor(ImageIcon icon, Point hotspot) 

Method Source Code

//package com.java2s;
/*//from  ww w . j a v  a2 s .c  o  m
 * Copyright appNativa Inc. All Rights Reserved.
 *
 * This file is part of the Real-time Application Rendering Engine (RARE).
 *
 * RARE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;

import java.awt.Toolkit;

import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

public class Main {
    /**
     * Creates a cursor from an ImageIcon
     *
     * @param icon the icon for the cursor
     * @param hotspot the cursor's hotspot
     *
     * @return the newly created cursor
     */
    public static Cursor createCursor(ImageIcon icon, Point hotspot) {
        if (icon == null) {
            return null;
        }

        return createCursor(icon.getImage(), hotspot, icon.getDescription());
    }

    /**
     * Creates a cursor from an image
     *
     * @param image the image for the cursor
     * @param hotspot the cursor's hotspot
     * @param name the name of the cursor
     *
     * @return the newly created cursor
     */
    public static Cursor createCursor(Image image, Point hotspot, String name) {
        int w = image.getWidth(null);
        int h = image.getHeight(null);
        Dimension size = Toolkit.getDefaultToolkit().getBestCursorSize(w, h);

        if ((size.width != w) || (size.height != h)) {
            BufferedImage b = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g = b.createGraphics();

            g.drawImage(image, 0, 0, null);
            image = b;
            g.dispose();
        }

        if (hotspot == null) {
            hotspot = new Point(0, 0);
        }

        return Toolkit.getDefaultToolkit().createCustomCursor(image, hotspot, name);
    }
}

Related

  1. componentToImageIcon(Component c, String description, boolean paintBorder)
  2. convert(ImageIcon icon)
  3. createBase64StringFromImage(ImageIcon image)
  4. createColorImageIcon(int w, int h, int c)
  5. createCursor(ImageIcon cursorIcon, Point hotSpot, String name)
  6. createDisabledImage(final ImageIcon imageIcon)
  7. createEmptyImageIcon(final int width, final int height)
  8. createFileImageIcon(final String path)
  9. createMonoColoredImageIcon(final Paint paint, final int width, final int height)