Friday, June 6, 2014
Drawing Line with Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing; // Add to draw
using System.Threading.Tasks;
namespace Line_with_Class
{
public class Line
{
private Point _p1;
private Point _p2;
private Color _color;
public Line(Point p1, Point p2, Color color)
{
_p1 = p1;
_p2 = p2;
_color = color;
}
public Point StartingPoint
{ get { return _p1; } }
public Point EndingPoint
{ get { return _p2; } }
private Color color;
public Color ColorUsing
{
get { return _color; }
set { color = value; }
}
}
}
//====================================================
namespace Line_with_Class
{
public partial class Form1 : Form
{
Bitmap bmap;
Graphics g;
List<Line> lineList = new List<Line>();
Point point1, point2;
Pen shapePen = new Pen(Color.Azure);
public Form1()
{
InitializeComponent();
bmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
g = Graphics.FromImage(bmap);
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
point1 = e.Location;
point2 = point1;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//Erase previous line
Pen pen = new Pen(pictureBox1.BackColor);
g.DrawLine(pen, point1, point2);
//Capture new Point
point2 = e.Location;
pen = new Pen(shapePen.Color);
g.DrawLine(pen, point1, point2);
pictureBox1.Image = bmap;
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
point2 = e.Location;
Line line = new Line(point1, point2, shapePen.Color);
lineList.Add(line);
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
foreach (Line line in lineList)
{
g.DrawLine(new Pen (line.ColorUsing), line.StartingPoint, line.EndingPoint);
pictureBox1.Image = bmap;
}
}
}
}
//1. Define class line
//2. Define List<Line>
//3. Mouse Up
// Create Line object, Save it List<line> Class Line
// Point p1
// Ponit p2
// Color C
// PictureBox1.Invalidate();
//4. pictureBox_Paint
// foreach(Line L in lines)
// { draw all the line }
Labels:
C#
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment