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.
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 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'.”
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With