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; }
}
}