如何在java中使图像逆时针旋转?
我有一个图像,我希望它沿逆时针方向移动,这就是我的问题。我有一个代码,但似乎我的代码无法正常工作。你可以检查我的代码是否有错误。请帮助我解决这个问题...
这是我的代码:
public void move(long dt)
{
double dt_s = dt / 1e9;
double dx_m = speed * dt_s;
double dy_m = speed * dt_s;
double width = board.x1_world;
double height = board.y1_world;
double min_height = 0.0;
double max_height = height;
double min_width = 0.0;
double max_width = width;
x += dx_m;
if (x >= max_width)
{
x = max_width;
if (y >= min_height)
{
y += dy_m;
}
}
if (y >= max_height)
{
y = max_height;
if (x >= min_width)
{
dx_m *= -1;
}
}
if (x <= min_width)
{
x = min_width;
if (y <= max_height)
{
y -= dy_m;
}
}
if (y <= min_height)
{
y = min_height;
if (x <= max_width)
{
dx_m *= -1;
}
}
}
@Override
public void render(Graphics2D g2d)
{
AffineTransform t = g2d.getTransform();
double height = 0.3;//meter
double width = 0.3;//meter
double bird_footy = height;
double bird_footx = width / 2;
int xx = board.convertToPixelX(x - bird_footx);
int yy = board.convertToPixelY(y + bird_footy);
g2d.translate(xx, yy);
double x_expected_pixels = width * board.meter;
double y_expected_pixels = height * board.meter;
double x_s = x_expected_pixels / ((ToolkitImage) birdImage).getWidth();
double y_s = y_expected_pixels / ((ToolkitImage) birdImage).getHeight();
g2d.scale(x_s, y_s);
g2d.drawImage(getImage(), 0, 0, this);
g2d.setColor(Color.BLACK);
g2d.setTransform(t);
}
I have an image and I want it to move in a counter-clockwise direction and that is my problem. I have a code but it seems my code doesn't work properly. You can check my code if where I've made my mistakes. Please help me regarding this matter...
This is my code:
public void move(long dt)
{
double dt_s = dt / 1e9;
double dx_m = speed * dt_s;
double dy_m = speed * dt_s;
double width = board.x1_world;
double height = board.y1_world;
double min_height = 0.0;
double max_height = height;
double min_width = 0.0;
double max_width = width;
x += dx_m;
if (x >= max_width)
{
x = max_width;
if (y >= min_height)
{
y += dy_m;
}
}
if (y >= max_height)
{
y = max_height;
if (x >= min_width)
{
dx_m *= -1;
}
}
if (x <= min_width)
{
x = min_width;
if (y <= max_height)
{
y -= dy_m;
}
}
if (y <= min_height)
{
y = min_height;
if (x <= max_width)
{
dx_m *= -1;
}
}
}
@Override
public void render(Graphics2D g2d)
{
AffineTransform t = g2d.getTransform();
double height = 0.3;//meter
double width = 0.3;//meter
double bird_footy = height;
double bird_footx = width / 2;
int xx = board.convertToPixelX(x - bird_footx);
int yy = board.convertToPixelY(y + bird_footy);
g2d.translate(xx, yy);
double x_expected_pixels = width * board.meter;
double y_expected_pixels = height * board.meter;
double x_s = x_expected_pixels / ((ToolkitImage) birdImage).getWidth();
double y_s = y_expected_pixels / ((ToolkitImage) birdImage).getHeight();
g2d.scale(x_s, y_s);
g2d.drawImage(getImage(), 0, 0, this);
g2d.setColor(Color.BLACK);
g2d.setTransform(t);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就这样:
ImageIO
辅助方法将图像加载为BufferedImage
,AffineTransformOp
对象,并给它一个旋转实例转换,最好是QuadrantRotateInstance
,因为您希望精确旋转 90°。3
表示 3 个象限,即旋转 3 次,每次 90°;相当于逆时针旋转一圈。BILINEAR
方法,这是半快半好质量的。过滤
你的转换;也就是说,将其应用到图像上。过滤返回一个新的 BufferedImage ,或者将新图像存储在给定的filter
的第二个参数中(上面的null
- 我们将图像存储在新的BufferredImage
对象)写入
或将图像保存到文件中。注释:
这是一个关于如何进行高效 90° 变换的非常简单的示例。当然这不是唯一的方法。获取此代码并适合您的代码。确保正确处理异常(与我的示例不同)。
如果您想了解更多信息,请阅读有关每个对象的 javadoc 的更多信息。
哎呀
,是的,所以,我发现 - 在阅读了 OP 的代码之后 - 这实际上是不相关的,因为 OP 似乎想要一个视觉效果 > 旋转,不仅仅是旋转图像并呈现它。
如果有人遇到这个问题并正在寻找这个,我会将其留在这里。
对于其余的,请忽略。
there you go:
BufferedImage
throughImageIO
helper methodsAffineTransformOp
object, and give it a rotate instance transformation, preferably anQuadrantRotateInstance
as you're interested in rotating by 90° exactly.3
there means 3 quadrants, so three times rotation of 90° each; that equals to one counter-clockwise rotation.BILINEAR
method, which is semi-fast and semi-good-quality.filter
your transform; that is, apply it to the image. A filtering returns a newBufferedImage
or it stores the new image in the second argument given tofilter
(null
above - we store the image in a newBufferredImage
object)write
or save your image to a file.Notes:
This is a very quick example on how you can do an efficient 90° transform. It's not the only way ofcourse. Take this code and fit it to yours. Make sure to correctly handle exceptions (unlike my example).
Read more on the javadoc about each of those objects if you want to know more.
Woops
yeah, sooo, I see that - after reading the code of OP - that this is unrelated actually, cause the OP seems to want a visual rotation, not just to rotate an image and present it.
I'll leave this here, if anyone bumps into this question and is looking for this.
For the rest, ignore it please.