Java2D - 如何旋转图像并保存结果

2022-01-16 00:00:00 image rotation java

我正在制作一个游戏,其中一些物体旋转以面对它们正在射击的物体.拍摄之间有延迟,我希望物体一直面向它所在的位置,直到再次拍摄.我知道如何加载图像,也知道如何使用 AffineTransform 旋转它们.但是有了这个,我需要在每次绘制对象时计算旋转.

I'm making a game where some objects rotate to face what they're shooting at. There's a delay in between shooting and I want the object to keep facing where it is until it shoots again. I know how to load images and I know how to rotate them using AffineTransform. But with this I need to calculate the rotate every time the object gets drawn.

所以我的问题是如何旋转图像并将结果保存到可以显示的新图像中?

So my question is how can I rotate an image and save the result into a new image that would get displayed?

推荐答案

如何旋转图像并将结果保存到可以显示的新图像中?

创建一个新的 BufferedImage.获取 Graphics 对象(通过 BufferedImage.getGraphics(). 将旋转后的图像绘制到这个缓冲图像上,并将图像保存在数组或基于它的旋转(以便在需要时轻松查找).

Create a new BufferedImage. Get hold of a Graphics object (through BufferedImage.getGraphics(). Paint the rotated image onto this buffered image, and save the image in an array or a map based on its rotation (so that it easy to look it up when you need it).

相关文章