28

I've been trying to figure out how to flip an image for a while, but haven't figured out yet.

I'm using Graphics2D to draw an Image with

g2d.drawImage(image, x, y, null)

I just need a way to flip the image on the horizontal or vertical axis.

If you want you can have a look at the full source on github.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Fuze
  • 423
  • 1
  • 6
  • 11

5 Answers5

64

From http://examples.javacodegeeks.com/desktop-java/awt/image/flipping-a-buffered-image:

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


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

// Flip the image vertically and horizontally; equivalent to 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);
image = op.filter(image, null);
I82Much
  • 26,901
  • 13
  • 88
  • 119
  • 1
    +1 for `-1` :-) A related example is mentioned [here](http://stackoverflow.com/a/9373195/230513). – trashgod Mar 04 '12 at 22:36
  • Works perfectly and is super simple! I use this with imageicons by simply converting them to buffered images https://stackoverflow.com/questions/15053214/converting-an-imageicon-to-a-bufferedimage – JFreeman Sep 19 '18 at 08:24
43

The easiest way to flip the image is by negative scaling it. Example:

g2.drawImage(image, x + width, y, -width, height, null);

That will flip it horizontally. This will flip it vertically:

g2.drawImage(image, x, y + height, width, -height, null);
samgak
  • 23,944
  • 4
  • 60
  • 82
hsirkar
  • 769
  • 1
  • 8
  • 19
  • 14
    This is almost good. It should be like g2.drawImage(image, x+height, y, width, -height, null); reason for this is because negative scale will move image to left, so you have to compensate this movement. – T.G Mar 23 '14 at 14:35
  • 2
    Yeah +1 @T.G for balancing out the negativity. – hsirkar Jan 10 '15 at 21:56
  • 1
    I believe this is more efficient than using AffineTransform. Anyone correct me if I am wrong. (+1) – user3437460 Oct 21 '15 at 17:22
3

You can use a transform on your Graphics, that should rotate the image just fine. Below is a sample code that you can use to acheive this:

AffineTransform affineTransform = new AffineTransform(); 
//rotate the image by 45 degrees 
affineTransform.rotate(Math.toRadians(45), x, y); 
g2d.drawImage(image, m_affineTransform, null); 
GETah
  • 20,922
  • 7
  • 61
  • 103
0

You need to know the width and height of the image to make sure it's properly scaled:

int width = image.getWidth(); int height = image.getHeight();

Then, you need to draw it:

//Flip the image both horizontally and vertically
g2d.drawImage(image, x+(width/2), y+(height/2), -width, -height, null);
//Flip the image horizontally
g2d.drawImage(image, x+(width/2), y-(height/2), -width, height, null);
//Flip the image vertically
g2d.drawImage(image, x-(width/2), y+(height/2), width, -height, null);

This is how I do it, anyways.

-1

Rotate Image Vertical 180 Degrees

File file = new File(file_Name);
FileInputStream fis = new FileInputStream(file);  
BufferedImage bufferedImage = ImageIO.read(fis); //reading the image file         
AffineTransform tx = AffineTransform.getScaleInstance(-1, -1);
tx.translate(-bufferedImage.getWidth(null), -bufferedImage.getHeight(null));
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
bufferedImage = op.filter(bufferedImage, null);
ImageIO.write(bufferedImage, "jpg", new File(file_Name));
Yash
  • 9,250
  • 2
  • 69
  • 74