java 对折线旋转
时间: 2023-10-17 21:13:02 浏览: 90
如果您想在 Java 中旋转折线,可以使用 Graphics2D 类的 rotate() 方法。这个方法可以将坐标系旋转一个指定的角度。
以下是一个简单的例子,展示了如何在 Java 中旋转折线:
``` java
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RotatedLine extends JPanel {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
// 定义折线的起点和终点
int x1 = 50;
int y1 = 50;
int x2 = 150;
int y2 = 150;
// 在起点和终点之间绘制折线
Line2D line = new Line2D.Double(x1, y1, x2, y2);
g2d.draw(line);
// 将坐标系旋转 45 度
g2d.rotate(Math.toRadians(45), x1, y1);
// 在旋转后的坐标系下重新绘制折线
g2d.draw(line);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Rotated Line");
frame.add(new RotatedLine());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
这个例子中,我们首先在默认的坐标系下绘制了一个折线,然后将坐标系旋转了 45 度,并在旋转后的坐标系下重新绘制了同样的折线。
阅读全文
相关推荐


















