Rafay Zia Mir
Rafay Zia Mir

Reputation: 2116

I need to erase the drawing drawn in a loop

Here is my code

while (true)
{
    gg = this.CreateGraphics();
    b1 = new SolidBrush(Color.Red);
    b2 = new SolidBrush(Color.Blue);
    p1 = new Pen(Color.Red);

    neuron.transferFunction_output();
    for (int i = 0; i < 10; i++)
    {
        if (neuron.layer2[i] == 1)
        {
            gg.FillEllipse(b1, ox1 - 100 * i, oy2, (float)20, (float)20);
            for (int j = 0; j < 10; j++)
            {
                gg.DrawLine(p1, ox1 - 100 * i, oy2, ox1 - 100 * (float)j, oy3);
            }
        }
        else
        {
            gg.FillEllipse(b2, ox1 - 100 * i, oy2, (float)20, (float)20);
        }
    }
    for (int i = 0; i < 10; i++)
    {
        if (neuron.layer3[i] == 1)
        {
            gg.FillEllipse(b1, ox1 - 100 * i, oy3, (float)20, (float)20);
            for (int j = 0; j < 10; j++)
            {
                gg.DrawLine(p1, ox1 - 100 * i, oy3, ox1 - 100 * (float)j, oy4);
            }
        }
        else
        {
            gg.FillEllipse(b2, ox1 - 100 * i, oy3, (float)20, (float)20);
        }
    }
    for (int i = 0; i < 10; i++)
    {
        if (neuron.layer4[i] == 1)
        {
            gg.FillEllipse(b1, ox1 - 100 * i, oy4, (float)20, (float)20);
            for (int j = 0; j < 10; j++)
            {
                gg.DrawLine(p1, ox1 - 100 * i, oy4, ox1 - 100 * (float)j, oy5);
            }
        }
        else
        {
            gg.FillEllipse(b2, ox1 - 100 * i, oy4, (float)20, (float)20);
        }
    }
    for (int i = 0; i < 10; i++)
    {
        if (neuron.layer5[i] == 1)
        {
            gg.FillEllipse(b1, ox1 - 100 * i, oy5, (float)20, (float)20);
        }
        else
        {
            gg.FillEllipse(b2, ox1 - 100 * i, oy5, (float)20, (float)20);
        }
    }
    Thread.Sleep(1000);
    Application.DoEvents();
}//end while

Now i want to wipe the drawings made earlier in the loop.Means i dont need to show last drawing, drawn in the last iteration. If i use Graphics.clear(Color.SomeColor) then the whole background color changes after each iteration and that i dont want to do that. Secondly if declare a method that draws same lines but with white color(means a method working as an eraser) on the same track as earlier then my code gets too long and its memory taking and time taking because i have to use Thread.Sleep() in my code due to some reasons. IS there any other way to erase the the last drawing? Thanks in regard

Upvotes: 1

Views: 602

Answers (1)

Ed Swangren
Ed Swangren

Reputation: 124642

while(true) {
    var g = someControl.CreateGraphics();
    draw(g);
}

That sort of construct will not work as well as one might hope. This is why you need to call DoEvents() in your loop; you're blocking the message pump. Also, you call CreateGraphics() and paint outside of the OnPaint handler, so anything you draw will be erased when the window performs a refresh.

You should move all of your drawing code into the OnPaint handler. This will give you a new surface to draw to every time you need it (call Invalidate() to force a re-paint) and will get you in line with the Windows message pump, so you can remove that call to (nasty) DoEvents(). You can still animate this way and your code becomes simpler.

You can update logical interactions within your loop and call Invalidate() when you're ready to paint the screen.

I would suggest some reading: The Windows Message Loop.

Upvotes: 4

Related Questions