A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 路好营 中级黑马   /  2015-10-31 22:42  /  1418 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.awt.*;
import javax.swing.*;
  class star extends JPanel {
    public  void star () {
        this.setSize(800, 800);
    }

    public void paint(Graphics g) {
        for (int i = 0; i < 100; i++) {
            int c1, c2, c3, w, h, r;
            c1 = (int) (Math.random() * 255);
            c2 = (int) (Math.random() * 255);
            c3 = (int) (Math.random() * 255);
            Color c = new Color(c1, c2, c3);// 随机生成颜色的红绿蓝通道值
            g.setColor(c);// 设置画笔颜色
            w = (int) (Math.random() * 1000);
            h = (int) (Math.random() * 800);
            r = (int) (Math.random() * 80);
            star(g, w, h, r, "fill");
        }

    }

    public static void star(Graphics g, int x0, int y0, int r, String f) {
        double ch = 72 * Math.PI / 180;// 圆心角的弧度数
        int x1 = x0, x2 = (int) (x0 - Math.sin(ch) * r), x3 = (int) (x0 + Math
                .sin(ch) * r), x4 = (int) (x0 - Math.sin(ch / 2) * r), x5 = (int) (x0 + Math
                .sin(ch / 2) * r);
        int y1 = y0 - r, y2 = (int) (y0 - Math.cos(ch) * r), y3 = y2, y4 = (int) (y0 + Math
                .cos(ch / 2) * r), y5 = y4;

        int bx = (int) (x0 + Math.cos(ch) * Math.tan(ch / 2) * r);
        int by = y2;

        Polygon a = new Polygon();
        Polygon b = new Polygon();

        a.addPoint(x2, y2);
        a.addPoint(x5, y5);
        a.addPoint(bx, by);
        b.addPoint(x1, y1);
        b.addPoint(bx, by);
        b.addPoint(x3, y3);
        b.addPoint(x4, y4);

        if (f.equals("draw")) {
            g.drawPolygon(a);
            g.drawPolygon(b);
        }
        if (f.equals("fill")) {
            g.fillPolygon(a);
            g.fillPolygon(b);
        }

    }

    public static void main(String[] args) {
        new f();
    }
}

class f extends JFrame {
    star a = new star();

    public f() {
        this.setSize(800, 800);
        setLocation(100, 100);
        this.add(a);
        this.setVisible(true);
    }

}


star.jpg (1.47 MB, 下载次数: 207)

star.jpg

2 个回复

倒序浏览
还能这么玩!!
回复 使用道具 举报

试试吧,很好玩的
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马