2016-11-20 69 views
-1

我想要采取这个代码我终于得到了绘制一颗星的工作,并且困惑于如何让它工作绘制25个不同的恒星(例如不同的侧面和spikinness,但我是不知道该如何去了解它到底。我想我会做一个新的随机变量int randomStars = (int)(Math.random()*25+1); // Variable for 25 Random Stars随机生成的明星,但我有点困惑在哪里把它从那里。Java:绘制“随机”星星

我会很感激的帮助。

我的代码(使用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 
     } 
} 

我的输出:

Current Output

+0

你想把它们贴在一起还是一个接一个? – Paul

+0

@Paul目标是彼此相邻,但我想随机的任何地方都可以工作,如果他们不是完全彼此顶部。 – Aramza

+0

好的。首先:您需要为每颗星创建两个数字(假设它们大小相同):半径和尖刺。然后使用'fillStar'方法渲染星星。只需循环插入星星的位置并将它们用作中心坐标即可。 – Paul

回答

0

上升为缓慢。首先专注于定位两颗星,只有两颗。选择星星中心的坐标并放置它们。调整以正确(您的第一次尝试可能会有错误)。测试,修复,再次测试,再次修复。重复。

然后玩尖刺和其他变化,所以星星是不一样的。经过测试和工作的时候,并且只有在这样的时候,才会转向三,四星等。随着更多的明星,你必须更小心避免重叠。