WPF Graphics.FillPolygon 在没有睡眠的情况下无法绘制多个多边形

发布于 2024-11-04 05:06:49 字数 1659 浏览 1 评论 0原文

下面的代码只有在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

草莓酥 2024-11-11 05:06:49

只是猜测:GetTriangle() 每次都会创建一个 Random 的新实例。

Just a guess: GetTriangle() creates a new instance of Random each time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文