我正在制作一个简单的图形编辑器(即绘画程序)。我没有计划制作任何花哨的东西,但我想让我的程序在进入绘画面板时将鼠标光标更改为类似于“+”或“O”的东西,就像Photoshop或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");