Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

Is it possible to use the fillPolygon method to make a star, and if so is there an easy way to do it?

+10 votes
I attempted this on my second project and it turned out a little wonky. Any suggestions?
asked Oct 5, 2016 in CSC 211 (F16) by Hunter Bader (325 points)

4 Answers

+4 votes
Yes it is possible to make a star with fillPolygon. In my opinion, it is better to scratch out the form of the star and use some maths to make the points to be in their right place.
answered Oct 5, 2016 by Minh Ta (1,045 points)
+4 votes
I would assume drawing a star would be possible using drawPolygon then plugging in all the points. This would be coordinates at each point and at each indent where the star caves in. Of course, this would require a bit of math, so it might be helpful to use graph paper to sketch it out as Stonedahl recommended.

Also, you could always draw a star using 5 drawLine commands but this probably wouldn't give the same effect for our project.
answered Oct 5, 2016 by Lars Harvey (526 points)
+3 votes
/**
* 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);
    }

}
answered Oct 10, 2016 by Jon Clauss (100 points)
+2 votes
You can use a polygon method and create a new Polygon with specified points at each point and angle of the star by defining those points for the shape!
answered Oct 6, 2016 by Dylan Hart (1,385 points)
...