Sumit Kumar Sumit Kumar - 4 months ago 12
Java Question

Flipping a BufferedImage horizontally

I'm using the following code to flip an image horizontally. It works, but the image is flipped horizontally and also vertically at the same time.
Right facing image, now faces left, but is also vertically flipped.
How can I stop the vertical flip?

public static BufferedImage createRotated( BufferedImage image)
AffineTransform at = AffineTransform.getRotateInstance(
Math.PI, image.getWidth()/2, image.getHeight()/2 );
return createTransformed(image, at);

public static BufferedImage createTransformed( BufferedImage image, AffineTransform at)
BufferedImage newImage = new BufferedImage(
image.getWidth(), image.getHeight(),
Graphics2D gg = newImage.createGraphics();
gg.drawImage(image, 0, 0, null);
return newImage;


Because you are rotating the image of 180 degrees, not flipping it.

Use the AffineTransform.getScaleInstance(-1, 1); instead.

EDIT: From here:

// Flip the image horizontally
tx = AffineTransform.getScaleInstance(-1, 1);
tx.translate(-image.getWidth(null), 0);

You must also translate the image because the scale will move the image to the left.