java - How to draw an 100 sided regular polygon -


i trying draw regular circle , 100 sided regular polygon. can draw regular circle:

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class shapes extends jframe implements actionlistener {      private jbutton button;     private jpanel panel;     public static void main(string[] args){      }          private void createline(){          setdefaultcloseoperation(exit-on-close);         container window=getcontentpane();         window.setlayout(new flowlayout());          panel=new jpanel();         panel.setpreferredsize(new dimension(700, 700));         panel.setbackground(color.white);         window.add(panel);          button=new jbutton("ok");         window.add(button);         button.addactionlistener(this);     }      public void actionperformed(actionevent event) {          graphics paper= panel.getgraphics();         int r = 75;         int x = 300;         int y = 150;          paper.drawoval(x,y,r,r);     } } 

i don't know how draw regular 100 sided polygon (do use loop function?)

can please me?

this general solution creating regular polygon n vertices (n - 1 sides). rectangle parameter setting bounds

basically, idea starting @ center of rectangle, rotate around area n vertices (360/n rotation step). source available here

public static polygon createpolygon(int vertices, double angleoffset, rectangle r) {         if (vertices < 1) throw new illegalargumentexception ("vertices must > 0");         double step = 2 * math.pi / vertices;         int[] x = new int[vertices];         int[] y = new int[vertices];         int xrad = r.width / 2;         int yrad = r.height / 2;         (int = 0; < vertices; i++) {             x[i] = r.x + xrad + (int) (math.cos(angleoffset + * step) * xrad);             y[i] = r.y + yrad + (int) (math.sin(angleoffset + * step) * yrad);         }         polygon p = new polygon(x, y, vertices);         return p;     } 

Popular posts from this blog