您需要使用三角函数来确定正确的宽度/高度,使用透明度来防止黑色区域,并且我认为“变换”是错误的,这使它偏离中心。
尝试这个:
public static BufferedImage rotate(BufferedImage image, double angle) {
double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
int w = image.getWidth(), h = image.getHeight();
int neww = (int)Math.floor(w*cos+h*sin), newh = (int) Math.floor(h * cos + w * sin);
GraphicsConfiguration gc = getDefaultConfiguration();
BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT);
Graphics2D g = result.createGraphics();
g.translate((neww - w) / 2, (newh - h) / 2);
g.rotate(angle, w / 2, h / 2);
g.drawRenderedImage(image, null);
g.dispose();
return result;
}
private static GraphicsConfiguration getDefaultConfiguration() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
return gd.getDefaultConfiguration();
}
来自http://flyingdogz.wordpress.com/2008/02/11/image-rotate-in-java-2-easier-to-use/
0
我正在寻找旋转图像。我有一个
JInternalFrame
,其中包含JLabel
。标签包含图像。旋转图像后,我需要调整内部框架的大小。我当前使用的代码旋转图像,但是图像边缘周围有黑色,并且偏心。对于如何解决这个问题,有任何的建议吗?