【发布时间】:2013-09-05 21:05:45
【问题描述】:
我正在为一个类项目用 Java 创建一个飞行雷达模拟器。
到目前为止,我已经能够展示许多以给定方向和不同速度在雷达上移动的小型飞机图像。
这条线显示了飞机移动的方向,应该指向的方向。
【问题讨论】:
标签: java rotation graphics2d
我正在为一个类项目用 Java 创建一个飞行雷达模拟器。
到目前为止,我已经能够展示许多以给定方向和不同速度在雷达上移动的小型飞机图像。
这条线显示了飞机移动的方向,应该指向的方向。
【问题讨论】:
标签: java rotation graphics2d
在 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;
}
【讨论】:
我假设你使用的是 Java2d,所以你应该看看AffineTransform class
【讨论】: