首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Graphics2D翻转图像

使用Graphics2D翻转图像
EN

Stack Overflow用户
提问于 2012-03-05 05:30:42
回答 3查看 51.3K关注 0票数 26

我已经试着找出如何翻转图像一段时间了,但还没有弄清楚。

我正在使用Graphics2D来绘制一个Image

代码语言:javascript
复制
g2d.drawImage(image, x, y, null)

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

如果你愿意,你可以看看full source on github

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-03-05 05:38:23

来自http://examples.javacodegeeks.com/desktop-java/awt/image/flipping-a-buffered-image

代码语言:javascript
复制
// 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);
票数 63
EN

Stack Overflow用户

发布于 2012-03-05 05:35:23

你可以在你的Graphics上使用一个变换,这应该可以很好地旋转图像。下面是一个示例代码,您可以使用它来实现这一点:

代码语言:javascript
复制
AffineTransform affineTransform = new AffineTransform(); 
//rotate the image by 45 degrees 
affineTransform.rotate(Math.toRadians(45), x, y); 
g2d.drawImage(image, m_affineTransform, null); 
票数 3
EN

Stack Overflow用户

发布于 2018-11-18 03:01:08

你需要知道图像的宽度和高度,以确保它被正确缩放:

代码语言:javascript
复制
int width = image.getWidth(); int height = image.getHeight();

然后,您需要绘制它:

代码语言:javascript
复制
//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);

不管怎么说,我就是这么做的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9558981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档