以下代码仅在使用Sleep(1)时绘制几个三角形,没有睡眠时它只绘制一个三角形:
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 :(得分:1)
只是猜测:GetTriangle()
每次都会创建Random
的新实例。