0

I aim to create a flexible progress bar, for use in a simple game. I am drawing a curve ( spline ), then extracting the points into a list. I then have an option: I can either use the last point to draw the position of the player - or connect all points up to the marker. Drawing the curve is successful. Extracting the points is successful. Drawing the points is successful. BUT AS SOON AS I ADD A BUTTON to the form, an unwanted line appears which joins the end of the curve to its beginning. Question : How do I prevent this unwanted line ? Sorry, I would like to add an image, but I cannot understand how to do so. Here is my code :-

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Point point = new Point(ovalPictMansion.Left - 30, ovalPictMansion.Top - 20);
            Size size = new Size(ovalPictMansion.Width + 60, ovalPictMansion.Height + 50);
            Rectangle rect = new Rectangle(point, size);
            Pen pen = new Pen(Color.DarkKhaki, 9);
            e.Graphics.DrawArc(pen, rect, 10, 160);

            DrawPath1(e.Graphics, point, pen);
        }

        GraphicsPath myPath = new GraphicsPath();

        private void DrawPath1(Graphics g, Point p, Pen pen)
        {
            Point[] points1 =
            {
                new Point(p.X + 10, p.Y + 120),
                new Point(p.X - 250, p.Y + 180),
                new Point(p.X - 380, p.Y + 390),
                new Point(p.X - 430, p.Y + 560),
                new Point(p.X - 520, p.Y + 700)
            };
            g.DrawCurve(pen, points1);

            myPath.AddCurve(points1);
            g.DrawPath(Pens.Red, myPath);

            using (var mx = new Matrix(1, 0, 0, 1, 0, 0))
            {
                myPath.Flatten(mx, 0.1f);
            }
            // store points in a list
            var list_of_points = new List<PointF>(myPath.PathPoints);


            //// Show position of points
            //foreach(PointF postnF in list_of_points)
            //{
            //    Point postn = new Point((int)postnF.X - 3, (int)postnF.Y - 3);
            //    Size size = new Size(6, 6);
            //    Rectangle rect = new Rectangle(postn, size);
            //    g.DrawEllipse(Pens.Red, rect);
            //}

            // Show position of last point only ( track the player )
            // Note : The start of the spline is the end of the Player's Path
            // So, show the first point ( instead of the last point )
            PointF postnF = list_of_points[0];
            Point postn = new Point((int)postnF.X - 3, (int)postnF.Y - 3);
            Size size = new Size(6, 6);
            Rectangle rect = new Rectangle(postn, size);
            g.DrawEllipse(Pens.Red, rect);
        }
Pete D.
  • 11
  • 2
  • Maybe you should clear the path from the previous points: `myPath.Reset();` before adding new points? _BUT AS SOON AS I ADD A BUTTON_ It is now so much adding the button as triggering an extra Paint event.. - As it stands you collect quite a number of points in the path, (mostly the same??) - But: If that is intentional you should use `myPath.StartFigure();` to separate the figures! - Minor issue: You are leaking a Pen ;-) – TaW Jun 06 '20 at 12:02

1 Answers1

0

BUT AS SOON AS I ADD A BUTTON to the form, an unwanted line appears which joins the end of the curve to its beginning.

This is because when you add a button it causes the form to repaint itself.

In DrawPath1(), you have myPath.AddCurve(points1);. This will add the points MULTIPLE times, once for each time the Paint() event fires. Thus the last point will draw to the first point because there are multiple sets of those points and the last point will be next to the first point in the newly added set...

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • I have now extended the mechanism to include six players. In doing so I needed to move the declaration of the GraphicsPath inside each DrawPath routine. This has resolved the problem - presumably because it automatically gives me an empty path each time. Thank you for your help. – Pete D. Jun 09 '20 at 12:47