Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flip Image with Graphics2D

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.

like image 396
Fuze Avatar asked Mar 04 '12 21:03

Fuze


People also ask

How to flip image using java?

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.

How do I flip an image vertically?

How do I flip my image? To flip an image or icon, simply: Select the object you would like to flip. Right click on that object, and select 'Flip Horizontal' or 'Flip Vertical'.”


1 Answers

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); 
like image 110
I82Much Avatar answered Oct 08 '22 08:10

I82Much