When I rotate an image using Graphics2D.rotate()
obviously it leaves some empty space in the corners. The empty corners become transparents.
I want my program to rotate the BufferedImage
and to fill the remaining empty corners with a white color.
How do I do this?
In other words, I want to rotate the image while preserving a white background for the whole image.
This is my function:
public BufferedImage rotateImage(BufferedImage image, double degreesAngle) {
int w = image.getWidth();
int h = image.getHeight();
BufferedImage result = new BufferedImage(w, h, image.getType());
Graphics2D g2 = result.createGraphics();
g2.rotate(Math.toRadians(degreesAngle), w/2, h/2);
g2.drawImage(image,null,0,0);
return result;
}
I then use this image to paint it a transparent JPanel, which I later add to a JLayeredPane.
However, my image has transparent corners and I want to fill them with a white color.