Flipping a Buffered Image horizontally, vertically - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Rotate

Description

Flipping a Buffered Image horizontally, vertically

Demo Code

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

public class Main {
  public static void main(String[] argv) {
    // Flip the image vertically
    BufferedImage image = null;/*from w  ww.  jav  a2 s.co m*/
    
    AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
    tx.translate(0, - image.getHeight(null));
    AffineTransformOp op = new AffineTransformOp(tx,
        AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    
    BufferedImage bufferedImage = null;
    bufferedImage = op.filter(bufferedImage, null);

    // Flip the image horizontally
    tx = AffineTransform.getScaleInstance(-1, 1);
    tx.translate(-image.getWidth(null), 0);
    op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    bufferedImage = op.filter(bufferedImage, null);

    // Flip the image vertically and horizontally;
    // rotating the image 180 degrees
    tx = AffineTransform.getScaleInstance(-1, -1);
    tx.translate(-image.getWidth(null), -image.getHeight(null));
    op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    bufferedImage = op.filter(bufferedImage, null);
  }

}

Related Tutorials