1

I am trying to draw a moving circle based on coordinates read from an XML file. At the moment the circle will only draw once. Could someone show me where I'm going wrong?!

The EyeMove method is called within a loop which reads the X and Y strings from the XML and parses to float

public void EyeMove(float x, float y)
{

    point = new PointF(x, y);
    Invalidate();


}

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    DrawCircle(e.Graphics, point.X, point.Y);
}

private void DrawCircle(Graphics g, float x, float y)
{

    using (Brush semiTransBrush = new SolidBrush(Color.Coral))
    {
        using (Pen pen = new Pen(Color.Aquamarine, 2))
        {
            g.DrawEllipse(pen, x, y, 50, 50);
            g.FillEllipse(semiTransBrush, x, y, 50, 50);

        }
    }
}
whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44
user8370201
  • 31
  • 1
  • 6
  • 1
    What happened when you debugged it? Did the loop execute more than once? Do the x/y co-ordinates get read correctly by the loop? – Equalsk Sep 04 '17 at 14:43
  • the loop executes more than once and the co ordinates are being read properly. It just doesn't look like the PointF information is being sent to the form to paint but I'm not sure why – user8370201 Sep 04 '17 at 14:46
  • Maybe it is executing too fast. So what you saw is the last drawn circle. Try to add a delay (~50ms) to each iteration of the loop if this is the case. – kennyzx Sep 04 '17 at 14:59
  • You were right. Thanks! – user8370201 Sep 04 '17 at 16:08

1 Answers1

1

The loop is executing too fast, the circles are drawn and erased too fast for human eyes to perceive, so what you saw is the last drawn circle. Try to add a delay (~50ms) to each iteration of the loop to slow it down.

The value of the delay is chosen to achieve a suitable FPS for the animation. This page provides some information on the frame rate and human vision.

kennyzx
  • 12,845
  • 6
  • 39
  • 83