Flip Image with Graphics2D
Asked Answered
A

5

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.

Aphonic answered 4/3, 2012 at 21:30 Comment(2)
Instead of having us look at your entire source, make an SSCCE.Menadione
If you google "Graphics2D rotate image" you'll find lots of tutorialsCovet
S
67

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);
Steeple answered 4/3, 2012 at 21:38 Comment(2)
+1 for -1 :-) A related example is mentioned here.Lotion
Works perfectly and is super simple! I use this with imageicons by simply converting them to buffered images #15053714Needlefish
R
44

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);
Refuse answered 11/11, 2013 at 17:44 Comment(3)
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.Wilkison
Yeah +1 @Wilkison for balancing out the negativity.Refuse
I believe this is more efficient than using AffineTransform. Anyone correct me if I am wrong. (+1)Kaka
T
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); 
Tracheid answered 4/3, 2012 at 21:35 Comment(0)
C
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.

Chesterchesterfield answered 17/11, 2018 at 19:1 Comment(0)
S
-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));
Spiritualize answered 6/7, 2015 at 6:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.