Estoy buscando rotar una imagen. Tengo un JInternalFrame
que contiene un JLabel
. La etiqueta contiene la imagen. Después de rotar la imagen, necesito cambiar el tamaño del marco interno. El código que tengo actualmente rota la imagen, pero hay negro alrededor de los bordes de la imagen y está desactivado. Alguna sugerencia en como arreglar esto?Girar una imagen en java
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
}
En general, la rotación de una imagen cambia la anchura y la altura (en relación con ejes X e Y). Supongo que esto está contribuyendo a que sea "fuera de foco". Tuve que resolver esto calculando el nuevo tamaño y contabilizándolo. En cuanto a los bordes negros, este es un caso bastante común en el que la función de rotación no funciona con un canal alfa. Tal vez esto ayude para el posicionamiento: http://stackoverflow.com/questions/2056338/calculating-the-center-of-rotation-after-translation – Jere
Véase también http://stackoverflow.com/questions/3420651 – trashgod