I'm making a simple graphics editor (i.e a paint program). I am not planning on anything fancy, but I do want my program to change the mouse cursor to something like a "+" or an "O" when it enters the Paint Panel. like in Photoshop or GIMP.
我该怎么做?我在 AWT / Swing 线程中找不到有关如何更改鼠标光标的任何内容。
以防万一有人想要比任何默认光标更“奇特”的东西:可以创建一个显示任意自定义图像的自定义光标(如果工具包支持它)。一个粗略的(没有闪亮的视觉效果)示例:
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension dim = kit.getBestCursorSize(48, 48);
BufferedImage buffered = GraphicsUtilities.createCompatibleTranslucentImage(dim.width, dim.height);
Shape circle = new Ellipse2D.Float(0, 0, dim.width - 1, dim.height - 1);
Graphics2D g = buffered.createGraphics();
g.setColor(Color.BLUE);
g.draw(circle);
g.setColor(Color.RED);
int centerX = (dim.width - 1) /2;
int centerY = (dim.height - 1) / 2;
g.drawLine(centerX, 0, centerX, dim.height - 1);
g.drawLine(0, centerY, dim.height - 1, centerY);
g.dispose();
Cursor cursor = kit.createCustomCursor(buffered, new Point(centerX, centerY), "myCursor");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)