如何在Java中围绕某一点旋转图像

huangapple go评论76阅读模式
英文:

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));

huangapple
  • 本文由 发表于 2020年9月14日 10:26:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63877342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定