我正在寻找旋转图像。我有一个JInternalFrame
包含一个JLabel
. 标签包含图像。图像旋转后,我需要调整内部框架的大小。我目前拥有的代码旋转图像,但图像边缘周围有黑色并且它偏离中心。对于如何解决这个问题,有任何的建议吗?
public void rotateIcon(int angle)
{
int w = theLabel.getIcon().getIconWidth();
int h = theLabel.getIcon().getIconHeight();
int type = BufferedImage.TYPE_INT_RGB; // other options, see api
BufferedImage DaImage = new BufferedImage(h, w, type);
Graphics2D g2 = DaImage.createGraphics();
double x = (h - w)/2.0;
double y = (w - h)/2.0;
AffineTransform at = AffineTransform.getTranslateInstance(x, y);
at.rotate(Math.toRadians(angle), w/2.0, h/2.0);
g2.drawImage(new ImageIcon(getData()).getImage(), at, theLabel);
g2.dispose();
theLabel.setIcon(new ImageIcon(DaImage));
this.setSize(DaImage.getWidth(),DaImage.getHeight()); //resize the frame
}