WPF Graphics.FillPolygon 在没有睡眠的情况下无法绘制多个多边形
下面的代码只有在使用 Sleep(1) 时才绘制多个三角形,如果不使用 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;
}
The following code draw several triangles only if with Sleep(1), without sleeping it draws only one triangle:
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);
}
}
Where is this code wrong?
Here is the code of routings:
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是猜测:
GetTriangle()
每次都会创建一个Random
的新实例。Just a guess:
GetTriangle()
creates a new instance ofRandom
each time.