0

I am facing an issue with Forms Graphics, as this is my first project on it. I'll try to simplify my issue as possible as I can.

I have a code in which, 1. A smaller circle revolves around bigger (// Animation 1) & 2. Drawing rectangle to form a wave line(// Animation 2).

    while (true)
        {
            // Issue line
            g.Clear(Color.White);

            g.DrawEllipse(pen, mainCircle);
            loc = nextPoint(radius, angle, cntr_mainCircle);

            innerCircle.X = loc.X - (innerCircle.Width / 2) + mainCircle.X;
            innerCircle.Y = loc.Y - (innerCircle.Height / 2) + mainCircle.Y;

            //Animation 1
            g.DrawEllipse(pen, innerCircle);
            //Animation 2
            g.DrawLine(pen, cntr_mainCircle.X + 30, cntr_mainCircle.Y + 30, innerCircle.X + innerCircle_lenX / 2, innerCircle.Y + innerCircle_lenY / 2);

            g.FillRectangle(Brushes.Red, startIndex, innerCircle.Y + 25, 3, 3);

            fg.DrawImage(btm, new PointF(20, 20));

            if (angle < 360)
            {
                angle += 1.0f;
            }
            else
            {
                angle = 0;
            }

            startIndex++;
        }

I am getting a problem with line g.Clear(Color.White);. It clears the entire screen, but I requirement is to clear // Animation 1 and not clear // Animation 2

Above code gives below output. The circle animation is correct as intended, but if you see.. there's a small RED rectangle which animates like a wave.. but because of Clear() the wave gets erased. enter image description here

I need a wave to look like below.(Below output is when I remove the // Issue line or say Clear() function) enter image description here

My expectation output is as below, i.e Circle animation (// Animation 1) which is clearing screen and doing motion animation & Wave animation (// Animation 2) which should not be clearing previously drawn rectangles.

enter image description here

I need suggestions to correct my code or I am even open for suggestions for applying a new approach if the current code is not upto the standards.

Wocugon
  • 566
  • 2
  • 7
  • 22
  • 2
    There's not just one thing wrong with this code. Never hang the UI thread with a while(true) loop, never use CreateGraphics(). You need to use the Paint event, a Timer to animate. Its Tick event should call Invalidate() to force a repaint. – Hans Passant Jan 12 '20 at 13:08
  • 2
    [Rotate Point around pivot Point repeatedly](https://stackoverflow.com/q/50473848/7444103) – Jimi Jan 12 '20 at 14:16
  • @HansPassant Hi, thanks for the advice, but using the approach you mentioned.. will I be able to solve the issue? Can I run 2 separate tickers and keep the graphic configuration separate for each animation? – Wocugon Jan 12 '20 at 16:37
  • @Jimi Don't have any problem running the rotation code, its working fine. :) – Wocugon Jan 12 '20 at 16:38
  • 2
    It's not about the rotation itself, but how the whole thing is rendered, in relation to the use of the `Graphics` object. I linked that code because a similar operation is performed. Also, as already mentioned, you don't use `while (true)` to draw in WinForms, you use the `Paint` event's `PaintEventArgs` object and `Invalidate()` the canvas (the Control you're drawing on). – Jimi Jan 12 '20 at 16:51
  • @Jimi Hi, do you have any reference or tutorial link for how to maintain the code structure for building graphic animations? – Wocugon Jan 19 '20 at 08:05

0 Answers0