Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Visual C# 2005
How to Program, 2/e

ISBN:
0-13-152523-9
© 2006
pages: ~1500

Order
Amazon logo

[Notes: This tutorial is an excerpt (Section 17.9) of Chapter 17, Graphics and Multimedia, from our textbook Visual C# 2005 How to Program, 2/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., Visual C# How to Program, 2/E ©2006. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

 

Two-Dimensional Graphics (Continued)
Fig. 17.22 Paths used to draw stars on a form.
1    // Fig. 17.22: DrawStarsForm.cs
2    // Using paths to draw stars on the form.
3    using System;
4    using System.Drawing;
5    using System.Drawing.Drawing2D;
6    using System.Windows.Forms;
7
8    // draws randomly colored stars
9    public partial class DrawStarsForm : Form
10   {
11       // default constructor
12       public DrawStarsForm()
13       {
14          InitializeComponent();
15       } // end constructor
16
17       // create path and draw stars along it
18       private void DrawStarsForm_Paint(object sender, PaintEventArgs e)
19       {
20          Graphics graphicsObject = e.Graphics;
21          Random random = new Random();
22          SolidBrush brush =
23             new SolidBrush( Color.DarkMagenta );
24
25          // x and y points of the path
26          int[] xPoints =
27             { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 };
28          int[] yPoints =
29             { 0, 36, 36, 54, 96, 72, 96, 54, 36, 36 };
30
31          // create graphics path for star;
32          GraphicsPath star = new GraphicsPath();
33
34          // create star from series of points
35          for ( int i = 0; i <= 8; i += 2 )
36             star.AddLine( xPoints[ i ], yPoints[ i ],
37                xPoints[ i + 1 ], yPoints[ i + 1 ] );
38
39          // close the shape
40          star.CloseFigure();
41
42          // translate the origin to (150, 150)
43          graphicsObject.TranslateTransform( 150, 150 );
44
45          // rotate the origin and draw stars in random colors
46          for ( int i = 1; i <= 18; i++ )
47          {
48             graphicsObject.RotateTransform( 20 );
49
50             brush.Color = Color.FromArgb(
51                random.Next( 200, 255 ), random.Next( 255 ),
52                random.Next( 255 ), random.Next( 255 ) );
53
54             graphicsObject.FillPath( brush, star );
55          } // end for
56       } // end method DrawStarsForm_Paint
57    } // end class DrawStarsForm
Page 1 | 2 | 3