- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Shape;
- import java.awt.geom.Line2D;
- import javax.swing.JFrame;
- public class nomain extends JFrame
- {
- private static final int x=200;
- private static final int y=50;
- private static final int width=300;
- public void drawMyPic()
- {
- this.setSize(800,600);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.show();
- }
- public void paint(Graphics g)
- {
- Shape l1=new Line2D.Double(x, y, x+width, y);
- Shape l2=new Line2D.Double(x+width, y, x+width+width/2, y+(width/2)*Math.sqrt(3));
- Shape l3=new Line2D.Double(x+width+width/2, y+(width/2)*Math.sqrt(3), x+width, y+2*((width/2)*Math.sqrt(3)));
- Shape l4=new Line2D.Double( x+width, y+2*((width/2)*Math.sqrt(3)), x, y+2*((width/2)*Math.sqrt(3)));
- Shape l5=new Line2D.Double(x, y+2*((width/2)*Math.sqrt(3)),(x-width/2), y+(width/2)*Math.sqrt(3));
- Shape l6=new Line2D.Double((x-width/2), y+(width/2)*Math.sqrt(3), x, y);
- Graphics2D gr=(Graphics2D)g;
- gr.draw(l1);
- gr.draw(l2);
- gr.draw(l3);
- gr.draw(l4);
- gr.draw(l5);
- gr.draw(l6);
- }
- public static void main(String[] args)
- {
- nomain m=new nomain();
- m.drawMyPic();
- }
- }
复制代码
注意这个画的是标注正六边形,如果显示不是标准正六边形很正常,就算法而言是是六边形 |