2016-11-20 252 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

上升爲緩慢。首先專注於定位兩顆星,只有兩顆。選擇星星中心的座標並放置它們。調整以正確(您的第一次嘗試可能會有錯誤)。測試,修復,再次測試,再次修復。重複。

然後玩尖刺和其他變化,所以星星是不一樣的。經過測試和工作的時候,並且只有在這樣的時候,纔會轉向三,四星等。隨着更多的明星,你必須更小心避免重疊。