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?


public BufferedImage rotateImageByDegrees(BufferedImage img, double angle) {

        double rads = Math.toRadians(angle);
        double sin = Math.abs(Math.sin(rads)), cos = Math.abs(Math.cos(rads));
        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;

        at.rotate(rads, x, y);
        g2d.drawImage(img, 0, 0, null);

        return rotated;


        g.drawImage(handler.rotateImageByDegrees(img, Direction), (int) x, (int) y, (int) width, (int) height, null);
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – bornander
    Aug 27, 2020 at 13:57

3 Answers 3


The following code fixed my problem:

        int newHeight = (int) Math.floor(height * Math.abs(Math.cos(Math.toRadians(Direction))) + width * Math.abs(Math.sin(Math.toRadians(Direction))));

        g.drawImage(handler.rotateImageByDegrees(img, Direction), (int) (x - ((newWidth - width) / 2)), (int) (y - ((newHeight - height) / 2)), newWidth, newHeight, null);```
  • \$\begingroup\$ Remember to click the ✅ icon to mark this answer as "Accepted" if it worked for you. \$\endgroup\$
    – DMGregory
    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.

what your code does:

  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);

  • 1
    \$\begingroup\$ This answer would be better if edited to include a brief explanation of how these lines of code solve the problem. \$\endgroup\$
    – DMGregory
    Feb 7 at 19:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .