Java 旋转光标或光标图像
好的。因此,我试图根据光标的位置旋转光标图像或光标本身。我试过使用Java 旋转光标或光标图像,java,image,rotation,cursor,degrees,Java,Image,Rotation,Cursor,Degrees,好的。因此,我试图根据光标的位置旋转光标图像或光标本身。我试过使用 Graphics2D g2d = (Graphics2D)g; AffineTransform old = g2d.getTransform(); g2d.rotate(Math.toRadians(degrees)); Toolkit toolkit = Toolkit.getDefaultToolkit(); //Get the default toolkit Image im
Graphics2D g2d = (Graphics2D)g;
AffineTransform old = g2d.getTransform();
g2d.rotate(Math.toRadians(degrees));
Toolkit toolkit = Toolkit.getDefaultToolkit(); //Get the default toolkit
Image image = toolkit.getImage("pictures/skills/skill" +InfoReader.SkillData("CastImage") +".png"); //Load an image for the cursor
Cursor cursor = toolkit.createCustomCursor(image, new Point(0, 0), "Cursor");
setCursor(cursor);
g2d.setTransform(old);
所以我认为这应该旋转图像,但是g2d.rotate()似乎对光标没有任何影响?我不能100%确定它是否对图像本身有影响。至少光标图像是我想要的
编辑:这里有一个视频示例:)(在我的例子中,我只想围绕某个始终保持在同一点上的点旋转它) 我觉得有点困惑,你想旋转什么 假设你有一个BuffereImage对象,你可以从中得到一个Graphics2D对象,通过对它进行操作,你可以得到你想要的
java.awt.image.BufferedImage buffImage=null;
试一试{
java.io.InputStream imageStream=
MyClass.class.getResourceAsStream(“图片/技能/技能”+InfoReader.SkillData(“CastImage”)+“.png”);
//MyClass是用作相对路径的任何类。。。
//使用ClassLoader.getSystemClassLoader().getResourceAsStream(…)
//绝对路径
buffImage=javax.imageio.imageio.read(imageStream);
}
catch(java.io.IOException | IllegalArgumentException ex){
//如果imageStream为null,则可能引发IllegalArgumentException。
}
Graphics2D g2d=buffImage.createGraphics();
试一试{
仿射变换old=g2d.getTransform();
g2d.旋转(数学.托拉迪安(度));
g2d.setTransform(旧);
}最后{
g2d.dispose();
}
Toolkit=Toolkit.getDefaultToolkit()//获取默认工具箱
Cursor Cursor=toolkit.createCustomCursor(图像,新点(0,0),“Cursor”);
设置游标(游标);
现在,如果你打算在使用它的时候旋转它,我不知道怎么做,但我希望我已经帮了一点忙
编辑:尝试查看此链接,它可能会帮助您:
编辑2:我现在知道你想要什么了,我不知道如何帮助你,但是试着看看我给你的链接(是的,它来自谷歌)。即使你找不到什么,它也可能对你的探索有所帮助。在搜索和询问类似问题的解决方案时,我找到了你的问题和答案。这是我在我的程序中使用的代码,它是有效的。请注意,此方法设计为只调用一次,不断调用可能需要优化。此外,我今天还学习了仿射变换,可能会犯一些错误(即使代码可以工作) 基本上,我旋转一个图像,从中创建一个新图像,并将新图像设置为光标。 我的“cursor.png”在数据文件夹中
private void rotateCursor(double rotation) {
// Get the default toolkit
Toolkit toolkit = Toolkit.getDefaultToolkit();
// Load an image for the cursor
BufferedImage image = null;
try {
image = ImageIO.read(this.getClass().getResource("/data/cursor.png"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
AffineTransform at = new AffineTransform();
// affineTransform applies the added transformations in the reverse order
// 3. translate it back to the center of the picture
at.translate(image.getWidth() / 2, image.getWidth() / 2);
at.rotate(rotation);//2- adds rotation to at (they are not degrees)
//1- translate the object so that you rotate it around the center
at.translate(-image.getWidth() / 2, -image.getHeight() / 2);
BufferedImage rotated = null; // creates new image that will be the transformed image
// makes this: at + image= rotated
AffineTransformOp affineTransformOp = new AffineTransformOp(at,
AffineTransformOp.TYPE_BILINEAR);
image2 = affineTransformOp.filter(image, rotated);
// Create the hotSpot for the cursor
Point hotSpot = new Point(10, 0); // click position of the cursor(ex: + shape's is middle)
Cursor cursor = toolkit.createCustomCursor(rotated, hotSpot, "cursor");
// Use the custom cursor
this.setCursor(cursor);
}
您可以使用window.getMousePosition().x
和window.getMousePosition().y代码>用于获取鼠标位置(如果您使用鼠标侦听器)
您需要使用正确的double调用rotateCursor()
方法。如何计算正确的双精度是我帮不了你的
我希望有帮助
我从以下来源了解到:
(本教程还有一个鼠标侦听器)