Java 鼠标光标图像未显示,重绘错误
Java mouse cursor image not showing up, repaint error
我为我的视频游戏创建了两个光标:
// blank cursor image
BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
blankCursor = toolkit.createCustomCursor(
cursorImg, new Point(0, 0), "blank cursor");
customCursor = toolkit.createCustomCursor(
MY_IMAGE, new Point(0,0) , "custom cursor");
我在启动游戏时将鼠标光标设置为空白:
this.setCursor(blankCursor);
然后,在某些情况下我想再次显示我的光标:
this.setCursor(customCursor);
空白光标有效。自定义光标永远不会出现。 (请注意,我知道该行是 运行,因为我使用的是 System.out.println
。我也知道光标图标有效,因为如果我将两者切换,则会显示 customCursor
和空白光标没有)
所以问题是:repainting/refreshing/revalidating 我需要做什么才能显示第二个光标图标?
此外,我很确定没有 setVisible()
游标方法,但如果有其他更好的解决方案,我们也欢迎。
不确定重绘或验证对您的情况是否有帮助:光标可能是由系统绘制的并且有一定的限制。在您的情况下,图像可能无效并变得透明,这就是 createCustomCursor 所做的:
Creates a new custom cursor object. If the image to display is
invalid, the cursor will be hidden (made completely transparent), and
the hotspot will be set to (0, 0).
我为我的视频游戏创建了两个光标:
// blank cursor image
BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
blankCursor = toolkit.createCustomCursor(
cursorImg, new Point(0, 0), "blank cursor");
customCursor = toolkit.createCustomCursor(
MY_IMAGE, new Point(0,0) , "custom cursor");
我在启动游戏时将鼠标光标设置为空白:
this.setCursor(blankCursor);
然后,在某些情况下我想再次显示我的光标:
this.setCursor(customCursor);
空白光标有效。自定义光标永远不会出现。 (请注意,我知道该行是 运行,因为我使用的是 System.out.println
。我也知道光标图标有效,因为如果我将两者切换,则会显示 customCursor
和空白光标没有)
所以问题是:repainting/refreshing/revalidating 我需要做什么才能显示第二个光标图标?
此外,我很确定没有 setVisible()
游标方法,但如果有其他更好的解决方案,我们也欢迎。
不确定重绘或验证对您的情况是否有帮助:光标可能是由系统绘制的并且有一定的限制。在您的情况下,图像可能无效并变得透明,这就是 createCustomCursor 所做的:
Creates a new custom cursor object. If the image to display is invalid, the cursor will be hidden (made completely transparent), and the hotspot will be set to (0, 0).