如何用Java程序写出五角星?

来源:百度知道 编辑:UC知道 时间:2024/09/22 09:50:53

第一种,用图形

import java.awt.*;
import javax.swing.*;

public class WuJiaoXing extends JPanel {

private static final long serialVersionUID = 1L;

private JFrame frame = null;

private int r = 150; // 外顶点外接圆半径

private int[] x = new int[5]; // 5个X外顶点坐标

private int[] y = new int[5]; // 5个Y外顶点坐标

private int[] x_ = new int[5]; // 5个X内顶点坐标

private int[] y_ = new int[5]; // 5个Y内顶点坐标

public WuJiaoXing() {
this.math();
frame = new JFrame("五角星");
frame.getContentPane().add(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setLocation(200, 200);

frame.setVisible(true);
}

private void math() {
int c = 360 / 5; // 角度
for (int i = 0; i < 5; i++) {