lundi 21 novembre 2016

Java: Drawing 25 different stars

I am stuck in my attempt to draw 25 random stars, I have a loop iterating threw but I don't understand why I am still only getting 1 star drawn instead of 25 different looking stars.

I'd really appreciate the help!

Note: Using DrawingPanel.java

My code:

import java.awt.*;
import java.util.Random;

public class StarSampler {

       public static void main(String[] args)
       {
           DrawingPanel panel = new DrawingPanel(1000, 1000);
           Graphics2D g = panel.getGraphics();
           g.setColor(Color.BLUE);
           panel.setBackground(new Color(250, 0, 0));

           Random rand = new Random();

           int xLoc = rand.nextInt(500) + 0; // int ctrX
           int yLoc = rand.nextInt(500) + 0; // int ctrY
           int starRadius = rand.nextInt(100) + 50; // int radius
           int starPoints = rand.nextInt(10) + 3; // int nPoints
           double starSpike = rand.nextInt(1) + .1; // double spikiness

           for (int randomStars = 0; randomStars <= 25; randomStars++)
           {
               fillStar(g, xLoc, yLoc, starRadius, starPoints, starSpike);
           }
       }

       public static void fillStar(Graphics2D g, int ctrX, int ctrY, int radius, int nPoints, double spikiness)
       {
           double xDouble[] = new double[2*nPoints];
           double yDouble[] = new double[2*nPoints];

           int xPoint[] = new int[2*nPoints]; 
           int yPoint[] = new int[2*nPoints];

           nPoints = (int) (nPoints * 2);

           for (int i = 0; i < nPoints; i++)
            {
                double iRadius = (i % 2 == 0) ? radius : (radius * spikiness);
                double angle = (270) + (i * 360.0) / (nPoints);

                xPoint[i] = (int) (ctrX + iRadius * Math.cos(Math.toRadians(angle)));
                yPoint[i] = (int) (ctrY + iRadius * Math.sin(Math.toRadians(angle)));
            }
                g.fillPolygon(xPoint, yPoint, nPoints); // Creates polygon
       }
}




Aucun commentaire:

Enregistrer un commentaire