英文:
How to rotate an image around a point in Java
问题
我一直在查看一些类似的问题,但没有一个确切地回答了我的需求。在我找到的解决方案中,每个人都是在不移动图像的情况下旋转图像,但我需要的是使该图像围绕初始位置旋转,而不是在原地旋转。
我使用的代码(我找到的):
AffineTransform transform = new AffineTransform();
transform.rotate(Math.toRadians(angle), img.getWidth() / 2, img.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
img = op.filter(img, null);
g.drawImage(img, getX(), getY(), null);
我需要图像相对于图像的第一个像素旋转。
英文:
I have been looking at some similar questions but none answer exactly what I need. In the solutions I found, everyone rotated the image without moving, but what I need is for this image to rotate around the initial position, and not for it to rotate in position.
Code I was using (what I found):
AffineTransform transform = new AffineTransform();
transform.rotate(Math.toRadians(angle), img.getWidth() / 2, img.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
img = op.filter(img, null);
g.drawImage(img, getX(), getY(), null);
I need the image to rotate in relation to the first pixel of the image.
答案1
得分: 1
rotate
方法调用中,将锚点传递为图像的中心点:
https://docs.oracle.com/javase/7/docs/api/java/awt/geom/AffineTransform.html#rotate(double,%20double,%20double)
尝试只传递旋转角度本身,它应该围绕左上角旋转:
transform.rotate(Math.toRadians(angle));
英文:
The rotate
method you’re calling passes in the anchor point as the center of the image:
https://docs.oracle.com/javase/7/docs/api/java/awt/geom/AffineTransform.html#rotate(double,%20double,%20double)
Try just passing in the rotation angle itself and it should rotate around the top-left:
transform.rotate(Math.toRadians(angle));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论