【JAVA语言程序设计基础篇】--图形--练习绘制多边形




package chapter15;

import java.awt.Graphics;
import java.awt.Polygon;

import javax.swing.*;
@SuppressWarnings("serial")
public class DrawPolygon extends JFrame{
	public DrawPolygon(){
		setTitle("drawpolygon");
		add(new PolygonsPanel());
	}
	public static void main(String[] args) {
		DrawPolygon frame = new DrawPolygon();
		frame.setLocationRelativeTo(null);
		frame.setTitle("aaa");
		frame.setSize(300, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

}
@SuppressWarnings("serial")
class PolygonsPanel extends JPanel{
	protected void paintComponent(Graphics g){
		super.paintComponent(g);
		
		int xCenter = getWidth()/2;
		int yCenter = getHeight()/2;
		int radius = (int)(Math.min(getWidth(), getHeight())*0.4);
		
		Polygon polygon = new Polygon();
		
		
		  // Add points to the polygon
	    polygon.addPoint(xCenter , yCenter);
	    polygon.addPoint((int)(xCenter + radius *
	      Math.cos(2 * Math.PI / 6)), (int)(yCenter - radius *
	      Math.sin(2 * Math.PI / 6)));
	    polygon.addPoint((int)(xCenter + radius *
	      Math.cos(2 * 2 * Math.PI / 6)), (int)(yCenter - radius *
	      Math.sin(2 * 2 * Math.PI / 6)));
	    polygon.addPoint((int)(xCenter + radius *
	      Math.cos(3 * 2 * Math.PI / 6)), (int)(yCenter - radius *
	      Math.sin(3 * 2 * Math.PI / 6)));
	    polygon.addPoint((int)(xCenter + radius *
	      Math.cos(4 * 2 * Math.PI / 6)), (int)(yCenter - radius *
	      Math.sin(4 * 2 * Math.PI / 6)));
	    polygon.addPoint((int)(xCenter + radius *
	      Math.cos(5 * 2 * Math.PI / 6)), (int)(yCenter - radius *
	      Math.sin(5 * 2 * Math.PI / 6)));

		g.drawPolygon(polygon);//开画
	}
	
}




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值