# How do I prevent buffered image size fluctuation when rotating?

As you can see in the GIF, as the car rotates, as the Car rotates, the size appears to change to fit the determined height and width, what would be the best way to change these in conjunction with the rotation. I have rotation saved as a variable from 0 to 359. I am using affine transform and a buffered image in Graphics2D. Is there an easy way to fix this in affine transform or when rendering?

rotator

public BufferedImage rotateImageByDegrees(BufferedImage img, double angle) {

int w = img.getWidth();
int h = img.getHeight();
int newWidth = (int) Math.floor(w * cos + h * sin);
int newHeight = (int) Math.floor(h * cos + w * sin);

BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = rotated.createGraphics();
AffineTransform at = new AffineTransform();
at.translate((newWidth - w) / 2, (newHeight - h) / 2);

int x = w / 2;
int y = h / 2;

g2d.setTransform(at);
g2d.drawImage(img, 0, 0, null);
g2d.dispose();

return rotated;
}


rendering

        g.drawImage(handler.rotateImageByDegrees(img, Direction), (int) x, (int) y, (int) width, (int) height, null);

• The (int) width, (int) height that you're passing to g.drawImage where are you getting them from? Is it the width and height of the rotated image or the width and height of the original image? It looks like you're drawing the rotated image using the dimensions of the original image, if you just leave those two parameters out I think it'll look as expected. Aug 27, 2020 at 13:57

The following code fixed my problem:



g.drawImage(handler.rotateImageByDegrees(img, Direction), (int) (x - ((newWidth - width) / 2)), (int) (y - ((newHeight - height) / 2)), newWidth, newHeight, null);$$$$

• Remember to click the ✅ icon to mark this answer as "Accepted" if it worked for you. Jan 17 at 11:39

The code you posted calculates the new height and width of the image after rotation, and then draws the image at a position adjusted by half the difference in size. This adjustment ensures that the image remains centered at its original position after rotation.

1. Math.abs(Math.cos(Math.toRadians(Direction))) and Math.abs(Math.sin(Math.toRadians(Direction))) calculate the absolute values of the cosine and sine of the rotation angle, respectively. These values are used to calculate the new dimensions of the image after rotation.
2. (int) Math.floor(height * Math.abs(Math.cos(Math.toRadians(Direction))) + width * Math.abs(Math.sin(Math.toRadians(Direction)))) calculates the new height of the image after rotation.
3. (int) (x - ((newWidth - width) / 2)) and (int) (y - ((newHeight - height) / 2)) calculate the new position at which to draw the image. This position is adjusted so that the image remains centered at its original position after rotation.
4. g.drawImage(handler.rotateImageByDegrees(img, Direction), (int) (x - ((newWidth - width) / 2)), (int) (y - ((newHeight - height) / 2)), newWidth, newHeight, null); draws the image at the calculated position and with the calculated dimensions.
BufferedImage rotatedImg = handler.rotateImageByDegrees(img, Direction);
g.drawImage(rotatedImg, (int) x, (int) y, rotatedImg.getWidth(), rotatedImg.getHeight(), null);`