使用Path绘制如下图所示三角形
时间: 2024-05-25 22:16:06 浏览: 139
```
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Triangle extends JPanel {
private static final long serialVersionUID = 1L;
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Path2D path = new Path2D.Float();
path.moveTo(50, 50);
path.lineTo(150, 50);
path.lineTo(100, 150);
path.closePath();
g2d.draw(path);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Triangle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Triangle());
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
运行结果如下图所示:
![image](https://user-images.githubusercontent.com/74979660/136699555-5ec9b5a6-a61c-4c8f-8c6a-7cd19b0c9b7b.png)
阅读全文