Animation isn't working because the picbox doesn't reset and the task doesn't sleep

Viewed 33

I have this code right here: EDIT: Source Code:

        private void btnAnimate_Click(object sender, EventArgs e)
        {
            Bitmap[] circle = new Bitmap[300];
            for (int i = 0; i < circle.Length; i++)
            {
                circle[i] = new Bitmap(260, 266);
            }
            double r = 25; // radius
            double rr = Math.Pow(r, 2); // r^2

            int h = 25; 
            // x value of centre of circle is represented with h and y value is represented with k
            for (int k = 25; k <= 100; k += 25) // y value of center moves down 1 pixel every iteration
            {
                
                for (int x = -h; x <= h + r; x++)
                {
                    for (int y = -k; y <= k + r; y++)
                    {
                        if (Math.Abs(Math.Pow(x - h, 2) + Math.Pow(y - k, 2)) <= rr)
                        // if: |(x-h)^2 + (y - h)^2| <= r^2, then draw the pixel
                        {
                           
                            circle[k - 25].SetPixel(x, y, Color.Red);
                            
                        }
                    }
                }
            }
            for (int l = 25; l <= 100; l+= 25)
            {
                picBox.Image = circle[l - 25];
                btnAnimate.Text = "circle" + (l/25);
                System.Threading.Thread.Sleep(1000);
            }
        }

(it use to be k++ but I made it k+= 25 so it would make it easier to trouble shoot, it will only draw 4 circles instead of the original 75.)

and I want it to draw a circle on a picture box (I've tested it and it can do that), but afterwards I want it to sleep for a while, then clear the picture box, draw a new circle only 1 pixel below it and repeat until the circle hits the ground. The problem with this is that the program is sleeping but it isn't showing the action done in between sleeps. i.e it says to show circle 1 then sleep until it reaches circle 4, but it will not show circles 1 through 3 though it will sleep, it will just show the final circle. It's not even just the pic box

you can see that I wrote

                btnAnimate.Text = "circle" + (l/25);
                System.Threading.Thread.Sleep(1000);

I want it to change the button text to 1, 2, 3, and 4 to count each iteration but it doesn't do that either. it will just show 4 at the end.

What do you think the problem is?

Solution:

            

            double r = 25; // radius
            double rr = Math.Pow(r, 2); // r^2

            int h = 25; 
            // x value of centre of circle is represented with h and y value is represented with k
            for (int k = 25; k <= 100; k += 25) // y value of center moves down 1 pixel every iteration
            {
                Bitmap circle = new Bitmap(260, 266);
                for (int x = -h; x <= h + r; x++)
                {
                    for (int y = -k; y <= k + r; y++)
                    {
                        if (Math.Abs(Math.Pow(x - h, 2) + Math.Pow(y - k, 2)) <= rr)
                        // if: |(x-h)^2 + (y - h)^2| <= r^2, then draw the pixel
                        {
                           
                            circle.SetPixel(x, y, Color.Red);
                            
                        }
                    }
                }
                picBox.Image = circle;
                picBox.Update();
                System.Threading.Thread.Sleep(100);
            }

1 Answers

You have two problems:

  1. You are not clearing the Bitmap circle drawn circle on each iteration, so you are building upon the previous drawn circle, so it looks like a falling circle with trails. You can fix this by defining your Bitmap circle inside of the first for loop, instead of outside.

  2. It is failing to show the new image inbetween Thread sleeps because you are not telling the control to refresh itself visually. Do this by calling picBox.Update() or picBox.Refresh() after you set picBox.Image = circle.

Related