如何使用 Java/Swing 旋转图像,然后将其原点设置为 0,0?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2687926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I rotate an image using Java/Swing and then set its origin to 0,0?
提问by JT.
I'm able to rotate an image that has been added to a JLabel. The only problem is that if the height and width are not equal, the rotated image will no longer appear at the JLabel's origin (0,0).
我能够旋转已添加到 JLabel 的图像。唯一的问题是,如果高度和宽度不相等,则旋转后的图像将不再出现在 JLabel 的原点 (0,0) 处。
Here's what I'm doing. I've also tried using AffineTransform and rotating the image itself, but with the same results.
这就是我正在做的事情。我也尝试过使用 AffineTransform 并旋转图像本身,但结果相同。
Graphics2D g2d = (Graphics2D)g;
g2d.rotate(Math.toRadians(90), image.getWidth()/2, image.getHeight()/2);
super.paintComponent(g2d);
If I have an image whose width is greater than its height, rotating that image using this method and then painting it will result in the image being painted vertically above the point 0,0, and horizontally to the right of the point 0,0.
如果我有一个宽度大于高度的图像,使用此方法旋转该图像然后绘制它会导致图像垂直绘制在点 0,0 上方,并水平绘制到点 0,0 的右侧。
回答by staticman
Use the g2d.transform() to shift the image back where it's needed. I'm just imagining what the calculation might be but I think something like:
使用 g2d.transform() 将图像移回需要的位置。我只是在想象计算可能是什么,但我认为是这样的:
int diff = ( image.getWidth() - image.getHeight() ) / 2;
g2.transform( -diff, diff );
BTW, you may have a problem with the label reporting its preferred size - you might need to override getPreferredSize() and account for swapping the images width and height if rotated.
顺便说一句,您可能在报告其首选大小的标签时遇到问题 - 您可能需要覆盖 getPreferredSize() 并考虑在旋转时交换图像的宽度和高度。
回答by Maurice Perry
AffineTransform trans = new AffineTransform(0.0, 1.0, -1.0, 0.0, image.getHeight(), 0.0);
g2d.transform(trans);
g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
回答by Aleksandr Movsesyan
The following function will rotate a buffered image that comes in indeterminate if it is a perfect square or not.
以下函数将旋转不确定的缓冲图像是否为完美正方形。
public BufferedImage rotate(BufferedImage image)
{
/*
* Affline transform only works with perfect squares. The following
* code is used to take any rectangle image and rotate it correctly.
* To do this it chooses a center point that is half the greater
* length and tricks the library to think the image is a perfect
* square, then it does the rotation and tells the library where
* to find the correct top left point. The special cases in each
* orientation happen when the extra image that doesn't exist is
* either on the left or on top of the image being rotated. In
* both cases the point is adjusted by the difference in the
* longer side and the shorter side to get the point at the
* correct top left corner of the image. NOTE: the x and y
* axes also rotate with the image so where width > height
* the adjustments always happen on the y axis and where
* the height > width the adjustments happen on the x axis.
*
*/
AffineTransform xform = new AffineTransform();
if (image.getWidth() > image.getHeight())
{
xform.setToTranslation(0.5 * image.getWidth(), 0.5 * image.getWidth());
xform.rotate(_theta);
int diff = image.getWidth() - image.getHeight();
switch (_thetaInDegrees)
{
case 90:
xform.translate(-0.5 * image.getWidth(), -0.5 * image.getWidth() + diff);
break;
case 180:
xform.translate(-0.5 * image.getWidth(), -0.5 * image.getWidth() + diff);
break;
default:
xform.translate(-0.5 * image.getWidth(), -0.5 * image.getWidth());
break;
}
}
else if (image.getHeight() > image.getWidth())
{
xform.setToTranslation(0.5 * image.getHeight(), 0.5 * image.getHeight());
xform.rotate(_theta);
int diff = image.getHeight() - image.getWidth();
switch (_thetaInDegrees)
{
case 180:
xform.translate(-0.5 * image.getHeight() + diff, -0.5 * image.getHeight());
break;
case 270:
xform.translate(-0.5 * image.getHeight() + diff, -0.5 * image.getHeight());
break;
default:
xform.translate(-0.5 * image.getHeight(), -0.5 * image.getHeight());
break;
}
}
else
{
xform.setToTranslation(0.5 * image.getWidth(), 0.5 * image.getHeight());
xform.rotate(_theta);
xform.translate(-0.5 * image.getHeight(), -0.5 * image.getWidth());
}
AffineTransformOp op = new AffineTransformOp(xform, AffineTransformOp.TYPE_BILINEAR);
BufferedImage newImage =new BufferedImage(image.getHeight(), image.getWidth(), image.getType());
return op.filter(image, newImage);
}
回答by camickr
The Rotated Iconclass might be what you are looking for.
该旋转图标类可能是你在找什么。

