Hi there,
I’m new to java 2D and have been having trouble rotating an image. The image i’m trying to rotate is stored as type .png, of type translucent as it has a transparent background. My problem is everytime i apply a rotation the image seems to get deformed resulting in a horrible swirly thing. Here’s the rotation methods i’ve been using.
public BufferedImage getRotatedImage(double deg, GraphicsConfiguration gc, boolean antialias) {
if (image == null) {
System.out.println("rotateImage: input image is null");
return null;
}
int w = image.getWidth();//null);
int h = image.getHeight();//(null);
int transparency = image.getColorModel().getTransparency();
BufferedImage bi = gc.createCompatibleImage(w, h, transparency);
Graphics2D g2d = (Graphics2D)bi.getGraphics();
g2d.setComposite(AlphaComposite.Src);
if(antialias) {
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
}
AffineTransform rotImg = new AffineTransform();
rotImg.rotate(Math.toRadians(deg), ((double)w)/2, ((double)h)/2); //Get a transform to rotate the image
g2d.transform(rotImg);
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
return(bi);
}
public void rotateImage(GraphicsConfiguration gc) {
int angle = 5;
BufferedImage rotImage = getRotatedImage(angle, gc, false);
image = rotImage;
}
i tried adding this to the code
AffineTransformOp op = new AffineTransformOp(rotImg, AffineTransformOp.TYPE_BILINEAR);
image = op.filter(image, bi);
But that seems to replace the deformed swirls with a blurred image due to the interpolation.
I’d appreciate any advice.
Cheers,
Spav