/**

* The following is a modification of something I wrote for Project 2.

* STOP READING NOW

* if you'd rather figure out how to draw stars yourself!

**/

import java.awt.*;

/**

* @author Jon Clauss

* @date 1 October 2016

*

* This program will draw an n-pointed star of a given size (radius) and given

* center (x,y). You can also choose the color.

*

* Please feel free to use/modify this class, but be sure to cite the author!

* [And let me know if you add a cool modification!]

*

*/

public class NStar {

public static void main(String[] args) {

DrawingPanel panel = new DrawingPanel(400, 400);

Graphics g = panel.getGraphics();

drawNStar(g, 200, 200, 150, 5, "B");

}

// This method takes a CENTER (x, y) (integer coordinates of the center) a

// radius r (double) an integer n and a color [denoted by a letter]and

// produces an n-pointed star.

public static void drawNStar(Graphics g, int x, int y, double r, int n, String color) {

if (color.equals("W")) {

g.setColor(Color.WHITE);

} else if (color.equals("R")) {

g.setColor(Color.RED);

} else if (color.equals("Y")) {

g.setColor(Color.YELLOW);

} else if (color.equals("G")) {

g.setColor(Color.GREEN);

} else {

g.setColor(Color.BLUE);

}

Polygon poly = new Polygon();

// Use trigonometry to plt the 2*n points needed

for (int i = 1; i <= 2 * n + 1; i++) {

poly.addPoint(x + (int) (r / (1.5 * (i % 2) + 1) * Math.cos((i * Math.PI / n) - (Math.PI / (2 * n)))),

y + (int) (r / (1.5 * (i % 2) + 1) * Math.sin((i * Math.PI / n) - (Math.PI / (2 * n)))));

}

// the factor of 1.5 inside (r / (1.5 * (i % 2) + 1) determines how "long" the points are;

// increasing say from 1.5 to 2 gives a more pointy star.

// You can change (Math.PI / (2 * n)) inside the trig functions [sin/cos] to rotate the star

g.fillPolygon(poly);

// optional: include a boundary

// g.setColor(Color.YELLOW);

// g.drawPolygon(poly);

}

}