import java.awt.Toolkit; //导入方法依赖的package包/类

/**

* 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);

try {

return tk.createCustomCursor(scaled, point,

"freeColDragIcon");

} catch (Exception ex) {

; // Fall through

}

}

return null;

}

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐