user16612111
user16612111

Reputation:

How to draw a line from point to point in C# with Panel, Graphics and Points

Problem

I have a list of points I desire to draw on a Panel, I have a method to check if the previous point is null, if it is not null then it is supposed to draw a new line from the previous point to the next point. Instead when I click on the button initialize_shape, the output I get is a single line connecting two points, the rest of the points are shown but no line exists between them.

Expectations

I expected the code to draw a line between each points such that it forms something like a pattern, with each point connected to the next with a line not just a single line.

Code

 public partial class Form1 : Form
    {
 //declare the lists that will hold the points
        List<Point> Shape1 = new List<Point>();
//this method belongs to a button object that draws the shape
 private void button1_Click(object sender, EventArgs e)
        {
            Shape1.Clear();
            Shape2.Clear();
            Point p1a = new Point(20, 30);
            Point p2a = new Point(120,50);
            Point p3a = new Point(160,80);
            Point p4a = new Point(180, 300);
            Point p5a = new Point(100,220);
            Point p6a = new Point(50, 280);
            Point p7a = new Point(20, 140);
            //Hold the Points in an array
            Point[] mypoints = new Point[] {p1a,p2a,p3a,p4a,p5a,p6a,p7a};
            //add the points to the List with one call
            Shape1.AddRange(mypoints);
            //the pens are added here
            Pen pBlue = new Pen(Brushes.Blue, 1);
            Pen pRED = new Pen(Brushes.Red, 1);
            //The panel object used below is a control that was added to the form
            Graphics g = panel1.CreateGraphics();
            DisplayShape(Shape1, pBlue, g);
           }
       //This method is supposed to draw lines between the points on the 
       //Panel
         void DisplayShape(List<Point> Shp,Pen pen, Graphics G)
        {
            Point? prevPoint = null;//nullable
            foreach(Point pt in Shp)
            {
                G.DrawEllipse(pen, new Rectangle(pt.X - 2, pt.Y - 2, 4, 4));
                //something in this logic is not right
                if (prevPoint != null)
                {
                    G.DrawLine(pen, (Point)prevPoint, pt);
                    prevPoint = pt;
                }
                G.DrawLine(pen, Shp[0], Shp[Shp.Count - 1]);
            }

        }
  //this method intializes the form
  public Form1()
        {
            InitializeComponent();   
        }
}

Output

enter image description here

Upvotes: 0

Views: 1564

Answers (1)

JonasH
JonasH

Reputation: 36341

This code

if (prevPoint != null)
{
      G.DrawLine(pen, (Point)prevPoint, pt);
      prevPoint = pt;
}

will never run, since prevPoint is only set inside the if-statement. It should be

if (prevPoint != null)
{
      G.DrawLine(pen, (Point)prevPoint, pt);
}
prevPoint = pt;

or skip the initial point

if(Shp.Count < 2) return;
var prevPoint = Shp[0];
foreach(Point pt in Shp.Skip(1)){
    G.DrawLine(pen, prevPoint, pt);
}

Also note that:

  • G.DrawLine(pen, Shp[0], Shp[Shp.Count - 1]); will always draw the same thing, so does not need to be inside the loop
  • Drawing inside a button event handler is probably not the way to go, since everything will be cleared when the control is redrawn. I would suggest attaching an event handler to the paint event. Or create a subclass of your panel an override OnPaint

Upvotes: 0

Related Questions