java – 用Graphics2D翻转图像

我一直试图找出如何翻转图像一段时间,但还没有想到。

我使用Graphics2D绘制一个图像

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

我只需要一种在水平或垂直轴上翻转图像的方法。

如果你想你可以看看github的完整的源(link)

编辑:Woops,我的意思是把“翻转”在标题中,而不是“旋转”。

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);
翻译自:https://stackoverflow.com/questions/9558981/flip-image-with-graphics2d

转载注明原文:java – 用Graphics2D翻转图像