问题描述
我做一个简单的图形编辑器(即一个的画图程序的)。我不打算对任何幻想,但我不希望我的计划,以鼠标光标更改为类似一个+或O,当它进入漆面板。像Photoshop或GIMP。
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的线程东西。
How would I do this? I can't find anything in AWT / Swing threads on how to change the mouse cursor.
推荐答案
万一有人想要更多的东西看中比任何默认光标:它可以创建一个自定义光标(提供的工具包支持的话)表示任意自定义图像。粗(没有光泽的视觉效果),例如:
Just in case someone wants something more "fancy" than any of the default cursors: it's possible to create a custom cursor (provided the Toolkit supports it) showing an arbitrary custom image. A crude (no shiny visuals) example:
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");
这篇关于如何改变光标图像中的Java AWT和/或Swing?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!