Java BufferedImage Flip flip(BufferedImage image, boolean flipHorizontal, boolean flipVertical)

Here you can find the source of flip(BufferedImage image, boolean flipHorizontal, boolean flipVertical)

Description

the graphic2d changes, must use the new one for future operations.

License

Open Source License

Declaration

public static BufferedImage flip(BufferedImage image, boolean flipHorizontal, boolean flipVertical) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Point;
import java.awt.geom.AffineTransform;

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

public class Main {
    /** the graphic2d changes, must use the new one for future operations. use image.createGraphics(); */
    public static BufferedImage flip(BufferedImage image, boolean flipHorizontal, boolean flipVertical) {
        Point scale = new Point(1, 1);
        Point translate = new Point(0, 0);
        if (flipHorizontal) {
            scale.x = -1;/*from  w  w w .j  a v a  2  s  .co  m*/
            translate.x = -image.getWidth();
        }
        if (flipVertical) {
            scale.y = -1;
            translate.y = -image.getHeight();
        }
        AffineTransform tx = AffineTransform.getScaleInstance(scale.x, scale.y);
        tx.translate(translate.x, translate.y);
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
        return op.filter(image, null);
    }
}

Related

  1. flip(BufferedImage image, int direction)
  2. flip(BufferedImage myImage, int type)
  3. flipAroundX(BufferedImage image)
  4. flipBothVerticallyAndHorizontallyWithAffineTransformOp(BufferedImage srcImage)