我正在为一个类项目在Java中创建一个飞行雷达模拟器。
到目前为止,我已经能够显示许多小飞机图像在雷达上移动,以一个给定的方向和不同的速度。
我的问题是如何旋转每一个飞机的图像,以跟随它的方向在雷达。

这条线显示了飞机移动的方向,应该是指向的。
发布于 2013-09-02 18:06:18
在Java中有几种方法可以做到这一点。AffineTransform可以工作,但我发现我无法轻松地调整图像大小以处理非90度旋转。我最终实现的解决方案如下。弧度角度。
public static BufferedImage rotate(BufferedImage image, double angle) {
double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
int w = image.getWidth();
int h = image.getHeight();
int newW = (int) Math.floor(w * cos + h * sin);
int newH = (int) Math.floor(h * cos + w * sin);
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.getDefaultConfiguration();
BufferedImage result = gc.createCompatibleImage(newW, newH, Transparency.TRANSLUCENT);
Graphics2D g = result.createGraphics();
g.translate((newW - w) / 2, (newH - h) / 2);
g.rotate(angle, w/2, h/2);
g.drawRenderedImage(image, null);
g.dispose();
return result;
}发布于 2013-09-02 17:59:44
我想您使用的是Java2d,所以您应该看看AffineTransform类
https://stackoverflow.com/questions/18578576
复制相似问题