0

我正在为一个类项目用 Java 创建一个飞行雷达模拟器。

到目前为止,我已经能够展示许多以给定方向和不同速度穿过雷达的小型飞机图像。

我的问题是如何旋转每个飞机图像以跟随雷达中的方向。 雷达

这条线显示了平面移动的方向并且应该指向。

4

2 回答 2

1

在 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-02T18:06:18.200 回答
0

我假设你正在使用 Java2d,所以你应该看看AffineTransform 类

于 2013-09-02T17:59:44.453 回答