我已经试着找出如何翻转图像一段时间了,但还没有弄清楚。
我正在使用Graphics2D
来绘制一个Image
g2d.drawImage(image, x, y, null)
我只需要一种方法来翻转水平或垂直轴上的图像。
如果你愿意,你可以看看full source on github。
发布于 2012-03-05 05:38:23
来自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);
发布于 2012-03-05 05:35:23
你可以在你的Graphics
上使用一个变换,这应该可以很好地旋转图像。下面是一个示例代码,您可以使用它来实现这一点:
AffineTransform affineTransform = new AffineTransform();
//rotate the image by 45 degrees
affineTransform.rotate(Math.toRadians(45), x, y);
g2d.drawImage(image, m_affineTransform, null);
发布于 2018-11-18 03:01:08
你需要知道图像的宽度和高度,以确保它被正确缩放:
int width = image.getWidth(); int height = image.getHeight();
然后,您需要绘制它:
//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);
不管怎么说,我就是这么做的。
https://stackoverflow.com/questions/9558981
复制相似问题