JAVA中绘制多边形的算法

时间:2022-07-12 09:03:21
 
  1 package lzy.di9zhang;
2
3 import java.awt.BorderLayout;
4 import java.awt.Graphics;
5 import java.awt.Point;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.awt.event.FocusEvent;
9 import java.awt.event.FocusListener;
10
11 import javax.swing.JButton;
12 import javax.swing.JFrame;
13 import javax.swing.JLabel;
14 import javax.swing.JPanel;
15 import javax.swing.JTextField;
16
17 public class Shiyan4 extends JFrame implements ActionListener, FocusListener {
18 private JLabel jl;
19 private JTextField jtf;
20 private JButton jb;
21 private JPanel jp2;
22 private MyPanel mp;
23
24 public Shiyan4() {
25 jl = new JLabel("请输入正多边形的边数:");
26 jtf = new JTextField(10);
27 jtf.addFocusListener(this);
28 jb = new JButton("确定");
29 jb.addActionListener(this);
30 mp = new MyPanel();
31 jp2 = new JPanel();
32 jp2.add(jl);
33 jp2.add(jtf);
34 jp2.add(jb);
35 this.add(mp);
36 this.add(jp2, BorderLayout.SOUTH);

37 this.setSize(500, 500);
38 this.setVisible(true);
39 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
40 }
41
42 public static void main(String[] args) {
43 Shiyan4 shiyan4 = new Shiyan4();
44 }
45
46 @Override
47 public void actionPerformed(ActionEvent e) {
48 if (e.getSource() == jb) {
49 String bianshu = jtf.getText();
50 mp.bianshu = Integer.parseInt(bianshu);
51 mp.repaint();
52 }
53 }
54
55 @Override
56 public void focusGained(FocusEvent e) {
57 if (e.getSource() == jtf) {
58 jtf.setText("");
59 }
60 }
61
62 public void focusLost(FocusEvent e) {
63 }
64 }
65
66 class MyPanel extends JPanel {
67 int bianshu;
68 private int bianshuMax = 20;
69 private int[] x = new int[bianshuMax];
70 private int[] y = new int[bianshuMax];
71 MyPolygon mplg = new MyPolygon(x, y);
72
73 public void paint(Graphics g) {
74 g.clearRect(0, 0, this.getWidth(), this.getHeight());//因为repaint不调用update,所以自己清空面板
75 if (bianshu <= bianshuMax) {
76 mplg.posOfPoint(bianshu);
77 g.drawPolygon(x, y, bianshu);
78 } else {
79 bianshuMax += 20;
80 x = new int[bianshuMax];
81 y = new int[bianshuMax];
82 mplg = new MyPolygon(x, y);
83 paint(g);
84 }
85 }
86 }
87
88 class MyPolygon {// 求正多边形的顶点坐标
89 private int[] x;
90 private int[] y;
91 private int startX;// 顶点的X坐标
92 private int startY;// 顶点的Y坐标
93 private int r;// 外接圆的半径
94
95 public MyPolygon(int[] x, int[] y) {
96 this.x = x;
97 this.y = y;
98 startX = 200;
99 startY = 10;
100 r = 200;
101 }
102
103 public void posOfPoint(int bianshu) {
104 x[0] = startX;
105 y[0] = startY;
106 Point p = new Point();
107 for (int i = 1; i < bianshu; i++) {
108 p = nextPoint(((2 * Math.PI) / bianshu) * i);
109 x[i] = p.x;
110 y[i] = p.y;
111 }
112 }
113
114 public Point nextPoint(double arc) {// arc为弧度,在顶点处建立直角坐标系,用r和arc确定下一个点的坐标
115 Point p = new Point();
116 p.x = (int) (x[0] - r * Math.sin(arc));
117 p.y = (int) (y[0] + r - r * Math.cos(arc));
118 return p;
119 }
120 }

核心代码:
 public  Point   nextPoint(double arc) {// arc为弧度,在顶点(x[0],y[0])处建立直角坐标系,

                      //用 r 和 arc 确定下一个点的坐标。
            Point p = new Point();
            p.x = (int) (x[0] - r * Math.sin(arc));
            p.y = (int) (y[0] + r - r * Math.cos(arc));
            return p;
           }

运行界面:

JAVA中绘制多边形的算法