Polygons are multisided shapes. There are several Graphics methods used to draw polygonsDrawLines draws a series of connected lines, DrawPolygon draws a closed polygon and FillPolygon draws a solid polygon. These methods are described in Fig. 17.19. The program in Fig. 17.20 allows users to draw polygons and connected lines via the methods listed in Fig. 17.19.
Method |
Description |
---|---|
DrawLines |
Draws a series of connected lines. The coordinates of each point are specified in an array of Point objects. If the last point is different from the first point, the figure is not closed. |
DrawPolygon |
Draws a polygon. The coordinates of each point are specified in an array of Point objects. If the last point is different from the first point, those two points are connected to close the polygon. |
FillPolygon |
Draws a solid polygon. The coordinates of each point are specified in an array of Point objects. If the last point is different from the first point, those two points are connected to close the polygon. |
Figure 17.20. Polygon-drawing demonstration.
1 // Fig. 17.20: DrawPolygons.cs 2 // Demonstrating polygons. 3 using System; 4 using System.Collections; 5 using System.Drawing; 6 using System.Windows.Forms; 7 8 // demonstrating polygons 9 public partial class PolygonForm : Form 10 { 11 // default constructor 12 public PolygonForm() 13 { 14 InitializeComponent(); 15 } // end constructor 1617 // contains list of polygon vertices 18 private ArrayList points = new ArrayList(); 19 20 // initialize default pen and brush 21 Pen pen = new Pen( Color.DarkBlue ); 22 SolidBrush brush = new SolidBrush( Color.DarkBlue ); 23 24 // draw panel mouse down event handler 25 private void drawPanel_MouseDown( object sender, MouseEventArgs e ) 26 { 27 // add mouse position to vertex list 28 points.Add( new Point( e.X, e.Y ) ); 29 drawPanel.Invalidate(); // refresh panel 30 } // end method drawPanel_MouseDown 31 32 // draw panel Paint event handler 33 private void drawPanel_Paint( object sender, PaintEventArgs e ) 34 { 35 // get graphics object for panel 36 Graphics graphicsObject = e.Graphics; 37 38 // if arraylist has 2 or more points, display shape 39 if ( points.Count > 1 ) 40 { 41 // get array for use in drawing functions 42 Point[] pointArray = 43 ( Point[] ) points.ToArray( points[ 0 ].GetType() ); 44 45 if ( lineOption.Checked ) 46 graphicsObject.DrawLines( pen, pointArray ); 47 else if ( polygonOption.Checked ) 48 graphicsObject.DrawPolygon( pen, pointArray ); 49 else if ( filledPolygonOption.Checked ) 50 graphicsObject.FillPolygon( brush, pointArray ); 51 } // end if 52 } // end method drawPanel_Paint 53 54 // handle clearButton click event 55 private void clearButton_Click( object sender, EventArgs e ) 56 { 57 points.Clear(); // remove points 58 drawPanel.Invalidate(); // refresh panel 59 } // end method clearButton_Click 60 61 // handle polygon RadioButton CheckedChanged event 62 private void polygonOption_CheckedChanged( 63 object sender, System.EventArgs e ) 64 { 65 drawPanel.Invalidate(); // refresh panel 66 } // end method polygonOption_CheckedChanged 67 68 // handle line ReadioButton CheckedChanged event 69 private void lineOption_CheckedChanged( 70 object sender, System.EventArgs e ) 71 { 72 drawPanel.Invalidate(); // refresh panel 73 } // end method lineOption_CheckedChanged 74 75 // handle filled polygon RadioButton CheckedChanged event 76 private void filledPolygonOption_CheckedChanged( 77 object sender, System.EventArgs e ) 78 { 79 drawPanel.Invalidate(); // refresh panel 80 } // end method filledPolygonOption_CheckedChanged 81 82 // handle colorButton Click event 83 private void colorButton_Click( object sender, EventArgs e ) 84 { 85 // create new color dialog 86 ColorDialog dialogColor = new ColorDialog(); 87 88 // show dialog and obtain result 89 DialogResult result = dialogColor.ShowDialog(); 90 91 // return if user cancels 92 if ( result == DialogResult.Cancel ) 93 return; 94 95 pen.Color = dialogColor.Color; // set pen to color 96 brush.Color = dialogColor.Color; // set brush 97 drawPanel.Invalidate(); // refresh panel; 98 } // end method colorButton_Click 99 } // end class PolygonForm |
To allow the user to specify a variable number of points, line 18 declares ArrayList points as a container for our Point objects. An ArrayList is similar to an array, but an ArrayList can grow dynamically to accommodate more elements. Lines 2122 declare the Pen and Brush used to color our shapes. The MouseDown event handler (lines 2530) for drawPanel stores mouse-click locations in points with ArrayList method Add (line 28). The event handler then calls method Invalidate of drawPanel (line 29) to ensure that the panel refreshes to accommodate the new point. Method drawPanel_Paint (lines 3352) handles the Panel's Paint event. It obtains the Panel's Graphics object (line 36) and, if the ArrayList points contains two or more Points (line 39), displays the polygon with the method that the user selected via the GUI radio buttons (lines 4550). In lines 4243, we extract an array from the ArrayList via method ToArray. Method ToArray can take a single argument to determine the type of the returned array; we obtain the type from the first element in the ArrayList by calling the element's GetType method.
Method clearButton_Click (lines 5559) handles the Clear button's Click event by calling ArrayList method Clear (causing the old list to be erased) and refreshing the display. Lines 6280 define the event handlers for each radio button's CheckedChanged event. Each method invalidates drawPanel to ensure that the panel is repainted to reflect the selected shape type. Event handler colorButton_Click (8398) allows the user to select a new drawing color with a ColorDialog, using the techniques demonstrated in Fig. 17.7.
Preface
Index
Introduction to Computers, the Internet and Visual C#
Introduction to the Visual C# 2005 Express Edition IDE
Introduction to C# Applications
Introduction to Classes and Objects
Control Statements: Part 1
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Polymorphism, Interfaces & Operator Overloading
Exception Handling
Graphical User Interface Concepts: Part 1
Graphical User Interface Concepts: Part 2
Multithreading
Strings, Characters and Regular Expressions
Graphics and Multimedia
Files and Streams
Extensible Markup Language (XML)
Database, SQL and ADO.NET
ASP.NET 2.0, Web Forms and Web Controls
Web Services
Networking: Streams-Based Sockets and Datagrams
Searching and Sorting
Data Structures
Generics
Collections
Appendix A. Operator Precedence Chart
Appendix B. Number Systems
Appendix C. Using the Visual Studio 2005 Debugger
Appendix D. ASCII Character Set
Appendix E. Unicode®
Appendix F. Introduction to XHTML: Part 1
Appendix G. Introduction to XHTML: Part 2
Appendix H. HTML/XHTML Special Characters
Appendix I. HTML/XHTML Colors
Appendix J. ATM Case Study Code
Appendix K. UML 2: Additional Diagram Types
Appendix L. Simple Types
Index