0$房天下问答|在Java中如何写五角星的小程序?$https://m.fang.com/ask/ask_1851205.html$https://static.soufunimg.com/common_m/m_public/201511/images/asksharedefault.png
packc/pages/ask/detail/detail?askid=1851205
共1个回答
-
-
-
陈晨G
丨Lv 4
import java.awt.*; import javax.swing.*; public class Test extends JPanel { private JFrame frame = null; private int r = 50; // 半径 private int[] x = new int[5]; // 5个X顶点坐标 private int[] y = new int[5]; // 5个Y顶点坐标 public Test() { this.math(); frame = new JFrame("五角星"); frame.getContentPane().add(this); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); frame.setVisible(true); } private void math() { int c = 360 / 5; // 角度 for (int i = 0; i < 5; i++) { x[i] = (int) (Math.cos(i * c * Math.PI / 30 - Math.PI / 2) * (r) + r); y[i] = (int) (Math.sin(i * c * Math.PI / 30 - Math.PI / 2) * (r) + r); } } public void paint(Graphics g) { super.paint(g); g.drawLine(x[0], y[0], x[2], y[2]); g.drawLine(x[0], y[0], x[3], y[3]); g.drawLine(x[1], y[1], x[3], y[3]); g.drawLine(x[1], y[1], x[4], y[4]); g.drawLine(x[2], y[2], x[4], y[4]); g.drawLine(x[2], y[2], x[0], y[0]); } public static void main(String[] args) { new Test(); } }参考文献:
免责声明:问答内容均来源于互联网用户,房天下对其内容不负责任,如有版权或其他问题可以联系房天下进行删除。

关注成功