FlorianSL
FlorianSL

Reputation: 89

Drawing lines on a panel

I try to make a graphics. When I click on my label, I want to draw a line. It works, it draw my line but at the last point there is another line going at the left top corner.. I don't know why.

(It's useless, but it's for another project, I try to understand how works the drawing)

Here's my code :

public partial class Form1 : Form
{
    Pen myPen = new Pen(Color.Blue);
    Graphics g = null;
    int start_x = 0, start_y;
    Point[] points = new Point[1000];
    int test = 0;

    public Form1()
    {
        InitializeComponent();
        start_y = canvas.Height / 2;
        points[0] = new Point (start_x,start_y);
        myPen.Width = 1;
    }

    private void drawLine()
    {

        g.DrawLines(myPen, points);
    }

    private void incrementation_Click(object sender, EventArgs e)
    {
        test = test + 1;
        incrementation.Text = test.ToString();
        if(test == 1)
        {
            points[1] = new Point(100, start_y);
        }
        if (test == 2)
        {
            points[test] = new Point(200, 90),new Point(220, 10);
        }
        if (test == 3)
        {
            points[test] = new Point(220, 10);
            drawLine();
        }

    }

    private void canvas_Paint(object sender, PaintEventArgs e)
    {
        g = canvas.CreateGraphics();
    }
}

Upvotes: 1

Views: 1356

Answers (2)

TaW
TaW

Reputation: 54433

A couple of issues.

  • You don't assign any values to points after points[3].

  • Point is a structure and will have a value of [0,0] at all further elements

  • so your lines go there.. (all 996 of them ;-)

There is more you should change:

  • Do the drawing in the Paint event or trigger it from there.

  • Do not store the Paint e.Grahpics object. You can pass it out to use it, but don't try to hold on to it.

  • After adding or changing the points, write canvas.Invalidate() to trigger the Paint event. This will make your drawing persistent.

To learn about persistent drawing minimize & restore the form!

  • Also you should use a List<Point> instead of an array. This lets you add Points without having to decide on the number of Points you want to support..

To create a new Point you write something like this:

points.Add(new Point(100, start_y) );

To draw you then use this format in the Paint event::

e.Graphics.DrawLines(myPen, points.toArray());

Upvotes: 1

Andrey Korneyev
Andrey Korneyev

Reputation: 26856

In the constructor you're filling first point as

points[0] = new Point (start_x,start_y);

At this moment, start_x = 0 (since you're not assigned anything else to it after declaration int start_x = 0).

Then in incrementation_Click you're assigning points[1], points[2] and points[3], but you don't changing anywhere in your code points[0].

So when you calling g.DrawLines - first point will always be (0, canvas.Height / 2)

Aside from this:

  1. You don't need to create graphics explicitly in _Paint event handler since it can accessed as e.Graphics.
  2. It's better to move all paintings into canvas_Paint like:

private void canvas_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawLines(myPen, points);
}

and in your _Click handler instead of calling drawLine you should only call canvas.Refresh()

Upvotes: 1

Related Questions