Java BufferedImage Flip flipImage(final BufferedImage image, final boolean horizontal, final boolean vertical)

Here you can find the source of flipImage(final BufferedImage image, final boolean horizontal, final boolean vertical)

Description

Flips an image horizontally and/or vertically.

License

Open Source License

Parameter

Parameter Description
image The image to be flipped.
horizontal Whether the image should be flipped horizontally.
vertical Whether the image should be flipped vertically.

Return

The given image, flipped horizontally and/or vertically.

Declaration

public static BufferedImage flipImage(final BufferedImage image, final boolean horizontal,
        final boolean vertical) 

Method Source Code


//package com.java2s;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

public class Main {
    /**/*from www  .j  a  v  a2  s .  c  om*/
     * Flips an image horizontally and/or vertically.
     *
     * @param image      The image to be flipped.
     * @param horizontal Whether the image should be flipped horizontally.
     * @param vertical   Whether the image should be flipped vertically.
     * @return           The given image, flipped horizontally and/or vertically.
     */
    public static BufferedImage flipImage(final BufferedImage image, final boolean horizontal,
            final boolean vertical) {
        int x = 0;
        int y = 0;
        int w = image.getWidth();
        int h = image.getHeight();

        final BufferedImage out = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        final Graphics2D g2d = out.createGraphics();

        if (horizontal) {
            x = w;
            w *= -1;
        }

        if (vertical) {
            y = h;
            h *= -1;
        }

        g2d.drawImage(image, x, y, w, h, null);
        g2d.dispose();

        return out;
    }
}

Related

  1. flipBufferedImage(BufferedImage bufferedImage)
  2. flipBufferedImageVertically(BufferedImage bufferedImage)
  3. flipHorizontal(BufferedImage bufferedImage)
  4. flipHorizontally(BufferedImage srcImage)
  5. flipImage(BufferedImage image)
  6. flipImage(Object original, boolean flipX, boolean flipY)
  7. flipImageHorizontally(BufferedImage image)
  8. flipImageVertically(BufferedImage theImage)
  9. flipVertical(BufferedImage im)