Drawing a star in AWT

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 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++){
}
}

public void draw(Graphics2D g2){
Point2D.Double r1
= new Point2D.Double(xCoordOfStar, yCoordOfStar);
// The front of the roof
Point2D.Double r2
= new Point2D.Double(xCoordOfStar,yCoordOfStar);
// The rear of the roof
Point2D.Double r3
= new Point2D.Double(xCoordOfStar, yCoordOfStar);
// The bottom of the rear windshield
Point2D.Double r4
= new Point2D.Double(xCoordOfStar,yCoordOfStar);
Point2D.Double r5
= new Point2D.Double(xCoordOfStar, yCoordOfStar);
// The front of the roof
Point2D.Double r6
= new Point2D.Double(xCoordOfStar, yCoordOfStar);
// The rear of the roof
Point2D.Double r7
= new Point2D.Double(xCoordOfStar, yCoordOfStar);
// The bottom of the rear windshield
Point2D.Double r8
= new Point2D.Double(xCoordOfStar, yCoordOfStar);
Point2D.Double r9
= new Point2D.Double(xCoordOfStar, yCoordOfStar);
// The front of the roof
Point2D.Double r10
= new Point2D.Double(xCoordOfStar, yCoordOfStar);
Point2D.Double r11
= new Point2D.Double(xCoordOfStar,yCoordOfStar);

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);

}

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.)

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, yCoordOfStar);
for (int i = 0; ++i < xCoordOfStar.length;)
g2.draw(new Line2D.Double(last, last = new Point2D.Double(xCoordOfStar[i], yCoordOfStar[i])));
}

I guess i figured it out!

I used :

g2.drawPolygon(xCoordOfStar, yCoordOfStar, POINTS);

it does the job with the least amount of code!

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