Java Code Examples for java.awt.Toolkit#getBestCursorSize()

The following examples show how to use java.awt.Toolkit#getBestCursorSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utilities.java    From netbeans with Apache License 2.0 7 votes vote down vote up
public static Cursor createCustomCursor(Component component, Image icon, String name) {
    Toolkit t = component.getToolkit();
    Dimension d = t.getBestCursorSize(16, 16);
    Image i = icon;

    if (d.width != icon.getWidth(null)) {
        if (((d.width) == 0) && (d.height == 0)) {
            // system doesn't support custom cursors, falling back
            return Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
        }

        // need to resize the icon
        Image empty = ImageUtilities.createBufferedImage(d.width, d.height);
        i = ImageUtilities.mergeImages(icon, empty, 0, 0);
    }

    return t.createCustomCursor(i, new Point(1, 1), name);
}
 
Example 2
Source File: KeyStoreEntryDragGestureListener.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Drag gesture recognized. Start the drag off if valid.
 *
 * @param evt
 *            Drag gesture event
 */
@Override
public void dragGestureRecognized(DragGestureEvent evt) {
	DragEntry dragEntry = kseFrame.dragSelectedEntry();

	if (dragEntry == null) {
		return;
	}

	ImageIcon icon = dragEntry.getImage();

	// Draw image as drag cursor
	Toolkit toolkit = Toolkit.getDefaultToolkit();
	Dimension dim = toolkit.getBestCursorSize(icon.getIconWidth(), icon.getIconHeight());
	BufferedImage buffImage = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB_PRE);
	icon.paintIcon(evt.getComponent(), buffImage.getGraphics(), 0, 0);
	cursor = toolkit.createCustomCursor(buffImage, new Point(0, 0), "keystore-entry");

	evt.startDrag(cursor, new KeyStoreEntryTransferable(dragEntry), this);
}
 
Example 3
Source File: DefaultTransferHandler.java    From freecol with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get a suitable cursor for the given component.
 *
 * @param c The component to consider.
 * @return A suitable {@code Cursor}, or null on failure.
 */
private Cursor getCursor(JComponent c) {
    if (c instanceof JLabel
        && ((JLabel)c).getIcon() instanceof ImageIcon) {
        Toolkit tk = Toolkit.getDefaultToolkit();
        ImageIcon imageIcon = ((ImageIcon)((JLabel)c).getIcon());
        Dimension bestSize = tk.getBestCursorSize(imageIcon.getIconWidth(),
            imageIcon.getIconHeight());
            
        if (bestSize.width == 0 || bestSize.height == 0) return null;
           
        if (bestSize.width > bestSize.height) {
            bestSize.height = (int)((((double)bestSize.width)
                    / ((double)imageIcon.getIconWidth()))
                * imageIcon.getIconHeight());
        } else {
            bestSize.width = (int)((((double)bestSize.height)
                    / ((double)imageIcon.getIconHeight()))
                * imageIcon.getIconWidth());
        }
        BufferedImage scaled = ImageLibrary
            .createResizedImage(imageIcon.getImage(),
                                bestSize.width, bestSize.height);
        Point point = new Point(bestSize.width / 2,
                                bestSize.height / 2);
        return tk.createCustomCursor(scaled, point,
                                     "freeColDragIcon");
    }
    return null;
}
 
Example 4
Source File: SwingXpraClient.java    From xpra-client with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onCursorUpdate(CursorPacket cursorPacket) {
	super.onCursorUpdate(cursorPacket);
	if(cursorPacket.isEmpty()) {
		return;
	}
	int width = cursorPacket.width;
	int height = cursorPacket.height;
	
	ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
   int[] nBits = {8, 8, 8, 8};
   int[] bOffs = {1, 2, 3, 0};
   ColorModel colorModel = new ComponentColorModel(cs, nBits, true, true,
                                        Transparency.TRANSLUCENT,
                                        DataBuffer.TYPE_BYTE);
   WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
                                           width, height,
                                           width*4, 4,
                                           bOffs, null);
   
	BufferedImage img = new BufferedImage(colorModel, raster, true, null);
	img.getRaster().setDataElements(0, 0, width, height, cursorPacket.pixels);
	
	Toolkit toolkit = Toolkit.getDefaultToolkit();
	Dimension size = toolkit.getBestCursorSize(width, height);
	Point hotspot = new Point(cursorPacket.xHotspot, cursorPacket.yHotspot);
	Image outputImg = img;
	if(size.width != width || size.height != height) {
		outputImg = img.getScaledInstance(size.width, size.height, Image.SCALE_SMOOTH);
		hotspot.x = hotspot.x * size.width / width;
		hotspot.y = hotspot.y * size.height / height;
	}
	
	Cursor c = toolkit.createCustomCursor(outputImg, hotspot, "test");
	SwingFrame window = (SwingFrame) getWindow(1);
	window.window.setCursor(c);
}
 
Example 5
Source File: EventObserver.java    From mochadoom with GNU General Public License v3.0 5 votes vote down vote up
/**
 * NASTY hack to hide the cursor.
 *
 * Create a 'hidden' cursor by using a transparent image
 * ...return the invisible cursor
 * @author vekltron
 */
private Cursor createHiddenCursor() {
    final Toolkit tk = Toolkit.getDefaultToolkit();
    final Dimension dim = tk.getBestCursorSize(2, 2);
    if (dim.width == 0 || dim.height == 0) {
        return this.initialCursor;
    }
    final BufferedImage transparent = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
    return tk.createCustomCursor(transparent, new Point(1, 1), "HiddenCursor");
}
 
Example 6
Source File: RangeFinderInteractor.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private static Cursor createRangeFinderCursor(ImageIcon cursorIcon) {
    Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
    final String cursorName = "rangeFinder";

    // this is necessary because on some systems the cursor is scaled but not the 'hot spot'
    Dimension bestCursorSize = defaultToolkit.getBestCursorSize(cursorIcon.getIconWidth(), cursorIcon.getIconHeight());
    Point hotSpot = new Point((7 * bestCursorSize.width) / cursorIcon.getIconWidth(),
                              (7 * bestCursorSize.height) / cursorIcon.getIconHeight());

    return defaultToolkit.createCustomCursor(cursorIcon.getImage(), hotSpot, cursorName);
}
 
Example 7
Source File: CursorX.java    From Forsythia with GNU General Public License v3.0 4 votes vote down vote up
static void setCursor(Grid grid){
Toolkit tk=Toolkit.getDefaultToolkit();
Dimension cdim=tk.getBestCursorSize(UI.GRID_CURSORXSIZE,UI.GRID_CURSORXSIZE);
Point hotspot=new Point(cdim.width/2,cdim.height/2);
Cursor c=tk.createCustomCursor(image,hotspot,NAME);
grid.setCursor(c);}
 
Example 8
Source File: CursorCircle.java    From Forsythia with GNU General Public License v3.0 4 votes vote down vote up
static void setCursor(Grid grid){
Toolkit tk=Toolkit.getDefaultToolkit();
Dimension cdim=tk.getBestCursorSize(UI.GRID_CURSORCIRCLESIZE,UI.GRID_CURSORCIRCLESIZE);
Point hotspot=new Point(cdim.width/2,cdim.height/2);
Cursor c=tk.createCustomCursor(image,hotspot,NAME);
grid.setCursor(c);}
 
Example 9
Source File: CursorSquare.java    From Forsythia with GNU General Public License v3.0 4 votes vote down vote up
static void setCursor(Grid grid){
Toolkit tk=Toolkit.getDefaultToolkit();
Dimension cdim=tk.getBestCursorSize(UI.GRID_CURSORSQUARESIZE,UI.GRID_CURSORSQUARESIZE);
Point hotspot=new Point(cdim.width/2,cdim.height/2);
Cursor c=tk.createCustomCursor(image,hotspot,NAME);
grid.setCursor(c);}