Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- public class Polygons {
- public static void main(String[] args) {
- DrawingPanel panel = new DrawingPanel(400,400);
- Graphics2D g = panel.getGraphics();
- }
- public static void fillRegularPolygon(Graphics2D g, int ctrX, int ctrY, int radius, int nPoints) {
- int [] yPoints = new int [nPoints];
- int [] xPoints = new int [nPoints];
- int angle = 360/nPoints;
- int pointCount = 0;
- for (int i = 0; i < 360; i+=angle) {
- double y = ctrY + radius * Math.sin(Math.toRadians(i-90));
- double x = ctrX + radius * Math.cos(Math.toRadians(i-90));
- yPoints[pointCount] = (int)Math.round(y);
- xPoints[pointCount] = (int)Math.round(x);
- pointCount++;
- }
- g.setColor(Color.BLACK);
- g.fillPolygon(xPoints, yPoints, nPoints);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement