dimanche 20 novembre 2016

Java: Drawing "Random" Stars

I'm trying to take this code I finally got working for drawing one star and am confused on how to get it working for drawing 25 different stars (such as different sides and spikinness but I am unsure how to go about it exactly. I assume I would make a new random variable int randomStars = (int)(Math.random()*25+1); // Variable for 25 Random Stars to randomly generate the stars but I'm kind of confused where to take it from there.

I'd appreciate the help.

My Code (Using DrawingPanel.java):

import java.awt.*;

public class StarSampler {

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

           fillStar(g, 250, 250, 150, 5, .3); // How to rotate it to start at center?
       }

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

           int randomStars = (int)(Math.random()*25+1); // Variable for 25 Random Stars

          // Would Nestest loop go here? for (randomStars++; randomStars < 25; randomStars++)
           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
       }
}

My Output:

Current Output




Aucun commentaire:

Enregistrer un commentaire