下面列出了java.awt.Toolkit#getBestCursorSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
/**
* 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);
}
/**
* 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;
}
@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);
}
/**
* 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");
}
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);
}
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);}
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);}
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);}