10.6 Drawing Connected Lines and Polygons

 <  Day Day Up  >  

You want to plot a series of points and connect the points using lines and optionally close the endpoints to create a polygon.


Technique

To draw a single line, use the DrawLine method. This method contains three parameters corresponding to the Pen , the starting Point value, and the ending Point value. You can optionally use four individual int or float values to specify the starting and ending coordinates.

A polygon is a series of connected lines whose starting and ending points are automatically connected. To create a polygon, call the DrawPolygon method, passing a Pen and an array of Point values. To create a filled polygon, call the FillPolygon method.

Listing 10.3 Drawing Lines and Polygons in Response to Clicking on a Panel Control
 using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace _6_LinesAndPolygons {     public class Form1 : System.Windows.Forms.Form     {         private System.Windows.Forms.Label label1;         private System.Windows.Forms.Panel panel1;         private Point lastPoint = new Point(-1,-1);         private ArrayList alPoints;         private System.ComponentModel.Container components = null;         public Form1()         {             InitializeComponent();             alPoints = new ArrayList();         }         protected override void Dispose( bool disposing )         {             if( disposing )             {                 if (components != null)                 {                     components.Dispose();                 }             }             base.Dispose( disposing );         }         // Windows Form Designer generated code         [STAThread]         static void Main()         {             Application.Run(new Form1());         }         private void panel1_MouseDown(object sender,             System.Windows.Forms.MouseEventArgs e)         {             Graphics g = panel1.CreateGraphics();             Random rand = new Random( DateTime.Now.Millisecond );             int rc = rand.Next(255);             int gc = rand.Next(255);             int bc = rand.Next(255);             if( e.Button == MouseButtons.Left )             {                 g.FillRectangle( new SolidBrush(Color.Black), e.X, e.Y, 5, 5 );                 if( lastPoint.X != -1 && lastPoint.Y != -1 )                 {                     g.DrawLine( new Pen(new SolidBrush(Color.Black)),                         lastPoint, new Point(e.X, e.Y ));                 }                 lastPoint.X = e.X;                 lastPoint.Y = e.Y;                 alPoints.Add( lastPoint );             }             else if( e.Button == MouseButtons.Right )             {                 Point[] points = (Point[]) alPoints.ToArray( typeof(Point));                 if( rand.Next(2) == 0 )                 {                     g.DrawPolygon( new Pen( new SolidBrush(                         Color.FromArgb(rc,gc,bc ))), points );                 }                 else                 {                     g.FillPolygon( new SolidBrush( Color.FromArgb(rc,gc,bc )),                         points );                 }                 alPoints.Clear();                 lastPoint.X = -1;                 lastPoint.Y = -1;             }             g.Dispose();         }     } } 

Comments

When it comes to actually drawing on a piece of paper, we find it extremely difficult to draw even a straight line. Any graphics work we do is on computers because we can draw extremely straight lines on it, provided we have the right tools to do the job. A line is a connection between any two given points. It comes as no surprise then that to draw a line in GDI+, you must specify two points. If you continue drawing lines starting from the last point and continuing to a new point, not only would you have a facsimile of a dot-to-dot puzzle, but you would also be one step closer to creating a polygon. By simply closing the first point with the last point in a series of connected lines, you create a polygon.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net