2011-05-02 42 views
0

下面的代碼畫幾個三角形只有當與睡眠(1),不睡它繪製只有一個三角形:WPF Graphics.FillPolygon不拉幾的多邊形不睡覺

public void Draw(Graphics g) 
     { 
      int count = 3; 

      for (int i = 0; i < count; i++) 
      { 
       System.Drawing.Color color = GetColor(); 
       System.Drawing.Point[] points = GetTriangle(); 

       g.FillPolygon(new System.Drawing.SolidBrush(color), points); 

       //System.Threading.Thread.Sleep(1); 
      } 
     } 

這是哪裏的代碼錯了嗎?
下面是路由的代碼:

private System.Drawing.Color GetColor() 
     { 
      Random rand = new Random((int)DateTime.Now.Ticks); 
      byte a = (byte)rand.Next(100); a += 155; 
      byte r = (byte)rand.Next(255); 
      byte g = (byte)rand.Next(255); 
      byte b = (byte)rand.Next(255); 

      return System.Drawing.Color.FromArgb(a, r, g, b); 
     } 

     private System.Drawing.Point[] GetTriangle() 
     { 
      Random rand = new Random((int)DateTime.Now.Ticks); 

      int x0 = rand.Next((int)IMAGE_W); 
      int y0 = rand.Next((int)IMAGE_H); 
      int x1 = rand.Next((int)IMAGE_W); 
      int y1 = rand.Next((int)IMAGE_H); 
      int x2 = rand.Next((int)IMAGE_W); 
      int y2 = rand.Next((int)IMAGE_H); 

      System.Drawing.Point x = new System.Drawing.Point(x0, y0); 
      System.Drawing.Point y = new System.Drawing.Point(x1, y1); 
      System.Drawing.Point z = new System.Drawing.Point(x2, y2); 
      System.Drawing.Point[] points = new System.Drawing.Point[] { x, y, z }; 

      return points; 
     } 
+0

[循環中的隨機數]可能的重複(http://stackoverflow.com/questions/3053807/random-number-in-a-loop) – 2011-05-02 11:09:12

回答

1

只是一個猜測:GetTriangle()創建的每次Random一個新的實例。

+0

哦,是的,和種子(int)DateTime.Now。蜱不起作用。謝謝! – purum 2011-05-02 11:07:15