Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. import java.awt.*;
  2.  
  3. public class Polygons {
  4.     public static void main(String[] args) {
  5.         DrawingPanel panel = new DrawingPanel(400,400);
  6.         Graphics2D g = panel.getGraphics();
  7.     }
  8.     public static void fillRegularPolygon(Graphics2D g, int ctrX, int ctrY, int radius, int nPoints) {
  9.         int [] yPoints = new int [nPoints];
  10.         int [] xPoints = new int [nPoints];
  11.         int angle = 360/nPoints;
  12.         int pointCount = 0;
  13.        
  14.         for (int i = 0; i < 360; i+=angle) {
  15.             double y = ctrY + radius * Math.sin(Math.toRadians(i-90));
  16.             double x = ctrX + radius * Math.cos(Math.toRadians(i-90));
  17.         yPoints[pointCount] = (int)Math.round(y);
  18.         xPoints[pointCount] = (int)Math.round(x);
  19.         pointCount++;
  20.         }
  21.         g.setColor(Color.BLACK);
  22.         g.fillPolygon(xPoints, yPoints, nPoints);
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement