Hi,

This is a part of my program, where I'm going to be able to draw lines, rectangles, ellipses, triangles and polygons in the end. With this code here I can draw a line on panel1, and it works nice. The downside is that I can't draw more than one line on the panel. Then the last line gets erased. This is because of the line g.Clear(Color.White) in the Draw method. But I need that line so that it doesn't draw a line for each time I move the mouse.
Do you know of a way to fix this?
Any help on the subject would be highly appriciated :)

public partial class Form1 : Form
{
        DLine line;

        System.Drawing.Graphics graphic;

        public Form1()
        {
            InitializeComponent();

            line = new DLine();
        }

private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (drawLineBtn.Checked)
            {
                line.PointOneX = e.X;
                line.PointOneY = e.Y;
            }	
	}

private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            graphic = panel1.CreateGraphics();

            if (e.Button == MouseButtons.Left)
            {
                if (drawLineBtn.Checked)
                {
                    line.PointTwoX = e.X;
                    line.PointTwoY = e.Y;
                    line.Draw(graphic);
                }
            }
         }
}

public class DLine : PenAndPosition
    {
        public void Draw(System.Drawing.Graphics g)
        {
            g.Clear(Color.White);
            g.DrawLine(pen, pointOneX, pointOneY, pointTwoX, pointTwoY);
            g.Dispose();   
        }
    }

public class PenAndPosition
    {
        public System.Drawing.Pen pen = new Pen(Color.Black);

        protected int pointOneX;
        protected int pointOneY;
        protected int pointTwoX;
        protected int pointTwoY;

        public PenAndPosition()
        {
            pen.Width = 2;
        }

        public int PointOneX
        {
            get { return pointOneX; }
            set { pointOneX = value; }
        }
        public int PointOneY
        {
            get { return pointOneY; }
            set { pointOneY = value; }
        }
        public int PointTwoX
        {
            get { return pointTwoX; }
            set { pointTwoX = value; }
        }
        public int PointTwoY
        {
            get { return pointTwoY; }
            set { pointTwoY = value; }
        }
    }

Here's a start: Instead of just declaring one line ( DLine line; ), why not make it a list of lines ( List<DLine> lines; )?

Use a container of the lines and then call them like this:
(Pseudo code)

Graphics g = this.GetGraphics();
g.Clear()

for each (line l in listOfLines)
l.Draw(g)

and in the line.draw method:

draw(graphics g)
{
g.drawline(line data);
}

Now when you run this the graphics object will be cleared once, then it will iterate the list and will draw the all the lines to the graphics object. You may want to look into double buffering in GDI+ once you have this all figuired out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.