Tidying up Your Lines with Endcaps

I l @ ve RuBoard

The line segments shown in Figure 3.5.1 have ends that are cut squarely and might not be good for drawing polygons. Figure 3.5.2 shows how wide lines with square ends look when used to draw a simple star.

Figure 3.5.2. Flat end caps used in a polygon.

graphics/0305fig02.gif

As you can see, the flat end caps make a mess of the shape where the lines join. To tidy this up, we can specify a rounded end cap for the lines. Pens have two properties, StartCap and EndCap , that can be set to draw specialized ends on your lines. In Listing 3.5.3, we show the program that draws the star and allows you to turn end caps on or off with a button on the form.

Listing 3.5.3 DrawStar.cs: The Line Cap Example
 1: using System;  2: using System.Drawing;  3: using System.Drawing.Drawing2D;  4: using System.Windows.Forms;  5:  6: class drawstar : System.Windows.Forms.Form  7: {  8:    Button button1;  9: 10:    bool EndCap; 11: 12:    public void OnPaint(object sender, PaintEventArgs e) 13:    { 14:       Pen pen=new Pen(Color.Black,(float)20.0); 15:       if(EndCap) 16:       { 17:          pen.StartCap=LineCap.Round; 18:          pen.EndCap=LineCap.Round; 19:       } 20: 21:       float r36 = (float)(36.0 * 3.1415926 / 180); 22:       Point Center=new Point(this.ClientRectangle.Width/2, 23:                             this.ClientRectangle.Height/2); 24:       float Pointsize = (float)0.8*Math.Min(Center.X,Center.Y); 25:       for(int i=0; i<10; i++) 26:       { 27:          float d1=(i & 1)==1 ? Pointsize / 2 : Pointsize; 28:          float d2=(i & 1)==0 ? Pointsize / 2 : Pointsize; 29:          e.Graphics.DrawLine(pen, 30:             new Point((int)(d1*Math.Cos(r36*i))+Center.X, 31:                       (int)(d1*Math.Sin(r36*i))+Center.Y), 32:             new Point((int)(d2*Math.Cos(r36*(i+1)))+Center.X, 33:                       (int)(d2*Math.Sin(r36*(i+1))+Center.Y))); 34:       } 35:       pen.Dispose(); 36:    } 37: 38:    public void OnSizeChanged(object sender, EventArgs e) 39:    { 40:       Invalidate(); 41:    } 42: 43:    public void OnClickedButton1(object sender, EventArgs e) 44:    { 45:       EndCap = !EndCap; 46:       Invalidate(); 47:    } 48: 49:    public drawstar() 50:    { 51:       this.Paint+=new PaintEventHandler(OnPaint); 52:       this.Text="Star"; 53:       this.SizeChanged+=new EventHandler(OnSizeChanged); 54: 55:       button1 = new Button(); 56:       button1.Location=new Point(5,5); 57:       button1.Size=new Size(50,20); 58:       button1.Text = "Caps"; 59:       button1.Click+=new EventHandler(OnClickedButton1); 60:       this.Controls.Add(button1); 61: 62:    } 63: 64:    static void Main() 65:    { 66:       Application.Run(new drawstar()); 67:    } 68: } ; 

Compile this program with the following command line:

 csc /t:winexe drawstar.cs 

When you run the program, you will see a button in the top-left corner of the window, click it to change to rounded end caps. There are 11 different line cap styles altogether, including a custom style.

Already you can see that GDI+ is a great improvement over good old trusty GDI, and we've only really looked at lines and rectangles. Let's now look at curves, paths, and splines.

I l @ ve RuBoard


C# and the .NET Framework. The C++ Perspective
C# and the .NET Framework
ISBN: 067232153X
EAN: 2147483647
Year: 2001
Pages: 204

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