I am looking to rotate an image. I have a JInternalFrame
which contains a JLabel
. The label contains the image. After the image has been rotated, I need to resize the internal frame. The code I have currently rotates the image, but there is black around the edges of the image and it is off centered. Any suggestions on how to fix this?
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
}
You could try using a Rotated Icon.
You need to be using trigonometry to determine the correct width/height, using transparency to prevent the black area, and I think the Transform is wrong, which is making it off center.
Try this:
from http://flyingdogz.wordpress.com/2008/02/11/image-rotate-in-java-2-easier-to-use/
Does it help if you change:
BufferedImage DaImage = new BufferedImage(height, width, type);
to:
BufferedImage DaImage = new BufferedImage(**width, height**, type);
?