Java BufferedImage Flip flip(BufferedImage image, int direction)

Here you can find the source of flip(BufferedImage image, int direction)

Description

Flip the image and return a new image

License

Open Source License

Parameter

Parameter Description
direction 0-nothing, 1-horizontal, 2-vertical, 3-both

Declaration

public static BufferedImage flip(BufferedImage image, int direction) 

Method Source Code

//package com.java2s;
/*/*from www. ja va 2s.  c  o m*/
 * Copyright (c) 2014 tabletoptool.com team.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     rptools.com team - initial implementation
 *     tabletoptool.com team - further development
 */

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

public class Main {
    /**
     * Flip the image and return a new image
     * @param direction 0-nothing, 1-horizontal, 2-vertical, 3-both
     * @return
     */
    public static BufferedImage flip(BufferedImage image, int direction) {
        BufferedImage workImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getTransparency());

        boolean flipHorizontal = (direction & 1) == 1;
        boolean flipVertical = (direction & 2) == 2;

        int workW = image.getWidth() * (flipHorizontal ? -1 : 1);
        int workH = image.getHeight() * (flipVertical ? -1 : 1);
        int workX = flipHorizontal ? image.getWidth() : 0;
        int workY = flipVertical ? image.getHeight() : 0;

        Graphics2D wig = workImage.createGraphics();
        wig.drawImage(image, workX, workY, workW, workH, null);
        wig.dispose();

        return workImage;
    }
}

Related

  1. flip(BufferedImage image, boolean flipHorizontal, boolean flipVertical)
  2. flip(BufferedImage myImage, int type)
  3. flipAroundX(BufferedImage image)
  4. flipBothVerticallyAndHorizontallyWithAffineTransformOp(BufferedImage srcImage)
  5. flipBufferedImage(BufferedImage bufferedImage)