2
\$\begingroup\$

I am new to java and graphics, and I am creating a java program that draws a star. It took me a few hours, but I finally have it drawing a star, but I am wondering if there is a better way to write the "draw" method without having to repeat myself over and over. I am trying to use loops to minimize all this code, but I am having trouble implementing it.

import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

/**
   A Star shape that can be positioned anywhere on the screen.
*/
public class Star
{
    private double x;
    private double y;
    private double radius;

    private final static double FACTOR = 2.63;
    private final static double ALFA = 18*Math.PI/180;
    private final static double BETA = 54*Math.PI/180;

    private final static double[] X = {0,Math.cos(BETA)/FACTOR, Math.cos(ALFA ),
                                            Math.cos(ALFA )/FACTOR, Math.cos(BETA),0,
                                            -Math.cos(BETA),-Math.cos(ALFA)/FACTOR,
                                            -Math.cos(ALFA ),-Math.cos(BETA)/FACTOR,0};

    private final static double[] Y = {-1,-Math.sin(BETA)/FACTOR, -Math.sin(ALFA ),
                                            Math.sin(ALFA )/FACTOR, Math.sin(BETA),1/FACTOR,
                                            Math.sin(BETA),Math.sin(ALFA )/FACTOR,
                                            -Math.sin(ALFA ),-Math.sin(BETA)/FACTOR,-1};

    private int[] xCoordOfStar; // x coordinates of the vertices of the star as pixel
    private int[] yCoordOfStar; // x coordinates of the vertices of the star as pixel           

     /**
    Calculates the coordinates of a 5 points star 
    @param radius, the length in pixel of each arm of the star from its center 
    @param x the x coordinate of the center of the star
    @param y the y coordinate of the center of the star
    */



    public Star(double radius , double x, double y){

        xCoordOfStar= new int[X.length];
        yCoordOfStar= new int[Y.length];
        int i; 
        for (i=0; i<X.length; i++){
            xCoordOfStar[i] = (int)(radius*X[i]+x);
            yCoordOfStar[i] = (int)(radius*Y[i]+y);
        }   
    }


    public void draw(Graphics2D g2){
    Point2D.Double r1 
            = new Point2D.Double(xCoordOfStar[0], yCoordOfStar[0]);
      // The front of the roof
      Point2D.Double r2 
            = new Point2D.Double(xCoordOfStar[1],yCoordOfStar[1]);
      // The rear of the roof
      Point2D.Double r3 
            = new Point2D.Double(xCoordOfStar[2], yCoordOfStar[2]);
      // The bottom of the rear windshield
      Point2D.Double r4 
            = new Point2D.Double(xCoordOfStar[3],yCoordOfStar[3]);
    Point2D.Double r5 
            = new Point2D.Double(xCoordOfStar[4], yCoordOfStar[4]);
      // The front of the roof
      Point2D.Double r6
            = new Point2D.Double(xCoordOfStar[5], yCoordOfStar[5]);
      // The rear of the roof
      Point2D.Double r7 
            = new Point2D.Double(xCoordOfStar[6], yCoordOfStar[6]);
      // The bottom of the rear windshield
      Point2D.Double r8 
            = new Point2D.Double(xCoordOfStar[7], yCoordOfStar[7]);
    Point2D.Double r9
            = new Point2D.Double(xCoordOfStar[8], yCoordOfStar[8]);
      // The front of the roof
      Point2D.Double r10 
            = new Point2D.Double(xCoordOfStar[9], yCoordOfStar[9]);
    Point2D.Double r11 
            = new Point2D.Double(xCoordOfStar[10],yCoordOfStar[10]);

    Line2D.Double first
            = new Line2D.Double(r1, r2);
        Line2D.Double second
            = new Line2D.Double(r2, r3);
        Line2D.Double third
            = new Line2D.Double(r3, r4);
    Line2D.Double fourth
            = new Line2D.Double(r4, r5);
        Line2D.Double fifth
            = new Line2D.Double(r5, r6);
        Line2D.Double sixth
            = new Line2D.Double(r6, r7);        
    Line2D.Double seventh
            = new Line2D.Double(r7, r8);
         Line2D.Double eigth
            = new Line2D.Double(r8, r9);
    Line2D.Double ninth
            = new Line2D.Double(r9, r10);
    Line2D.Double tenth
        = new Line2D.Double(r10,r11);


      g2.draw(first);
      g2.draw(second);
      g2.draw(third);
      g2.draw(fourth);      
      g2.draw(fifth);      
      g2.draw(sixth); 
      g2.draw(seventh);
      g2.draw(eigth);
      g2.draw(ninth);
      g2.draw(tenth);      


    }
\$\endgroup\$
3
\$\begingroup\$

Rather than having two separate arrays for x and y coordinates, you should consider an array of Points.

As for your loops question, when creating the points, your loop can look like this:

points[i] = new Point2D.Double(xCoordOfStar[i], yCoordOfStar[i]);

and then your lines (probably in a separate loop):

lines[i] = new Line2D.Double(points[i],points[i+1]);
g2.draw(lines[i]);

(Note that this works because you have 11 points for 10 lines, and your first and last points match.)

\$\endgroup\$
1
\$\begingroup\$

You don't need references to all of the points, you are only re-using the last used point, thus the method can be reduced to:

public void draw(
        final Graphics2D g2) {
    ////
    Point2D.Double last = new Point2D.Double(xCoordOfStar[0], yCoordOfStar[0]);
    for (int i = 0; ++i < xCoordOfStar.length;)
        g2.draw(new Line2D.Double(last, last = new Point2D.Double(xCoordOfStar[i], yCoordOfStar[i])));
}
\$\endgroup\$
1
\$\begingroup\$

I guess i figured it out!

I used :

g2.drawPolygon(xCoordOfStar, yCoordOfStar, POINTS);

it does the job with the least amount of code!

\$\endgroup\$
  • 2
    \$\begingroup\$ How does this translate to the given FACTOR, ALFA and BETA? \$\endgroup\$ – Mast Oct 1 '16 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.