用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

java 画五角星 填充五角星

2013-05-27 作者: 小蜜锋举报

[java]代码库

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

public class WuJiaoXing extends JPanel {
	public WuJiaoXing() {
		this.setSize(800, 800);
	}

	public void paint(Graphics g) {
		wujiaoxing(g, 200, 200, 100, "draw");
		wujiaoxing(g, 400, 400, 100, "fill");
	}

	public void wujiaoxing(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 g();
	}
}

class g extends JFrame {
	WuJiaoXing a = new WuJiaoXing();

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

}

[代码运行效果截图]


java 画五角星 填充五角星


网友评论    (发表评论)

共2 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...