这个方法,怎么实现啊。
定义一个类,类中包含适当的属性和一个方法,该方法的参数为点的坐标值,当有一个点时,该方法划一个点;当有两个点时,在两点中间画一条直线;当有三个点时,画一个三角形。
望大家给个全代码,我仔细琢磨,还不懂的我会续贴的。
一直在C板块逛的,来学习C#,忘大家指教。
class Drawer { private List<Point> pointGroup = new List<Point>(); private System.Drawing.Graphics graphics; private System.Windows.Forms.Control control; public Drawer(System.Windows.Forms.Control control) { this.control = control; graphics = control.CreateGraphics(); } public void Draw(int x, int y) { if (pointGroup.Count == 3) { pointGroup.Clear(); control.Refresh(); } Point p = new Point(x, y); Size size = new Size(1, 1); Pen pen = new Pen(System.Drawing.Brushes.Black); Rectangle rect = new System.Drawing.Rectangle(p, size); pointGroup.Add(p); if (pointGroup.Count == 1) { graphics.DrawEllipse(pen, rect); } else if (pointGroup.Count == 2) { graphics.DrawEllipse(pen, rect); graphics.DrawLine(pen, pointGroup[0], pointGroup[1]); } else if (pointGroup.Count == 3) { graphics.DrawEllipse(pen, rect); graphics.DrawLine(pen, pointGroup[1], p); graphics.DrawLine(pen, pointGroup[0], p); } } }
using System.Drawing; using System.Windows.Forms; namespace Test { class MyPointPaint:Panel { private Point[] P; // p 为坐标集合 public MyPointPaint(Point[] p):base() { this.P = p; this.Size = new Size(new Point(400, 400)); } // 绘制 protected override void OnPaint(PaintEventArgs e) { if (this.P.Length < 1) return; base.OnPaint(e); Graphics g = e.Graphics; Pen pen =new Pen(SystemBrushes.GrayText); if (this.P.Length == 3) { g.DrawPolygon(pen, this.P); // 绘制三角形 } else if (this.P.Length == 1) { g.DrawEllipse(pen, new Rectangle(this.P[0].X, this.P[0].Y, 2, 2)); // 绘制一个圆点 } else if (this.P.Length == 2) { g.DrawLine(pen, this.P[0], this.P[1]); // 绘制一条直线 } else { g.DrawLines(pen, this.P); // 绘制所有坐标结构的线段 } g.Flush(); } } } // Test code Point p1 = new Point(100, 100); Point p2 = new Point(200, 200); Point p3 = new Point(300, 150); Point[] ps = new Point[3]; ps[0] = p1; ps[1] = p2; ps[2] = p3; MyPointPaint pp = new MyPointPaint(ps); this.Controls.Add(pp);