17.8. Drawing Polygons and Polylines 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. Figure 17.19. Graphics methods for drawing polygons.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: FrmDrawPolygons.vb 2 ' Demonstrating polygons. 3 Public Class FrmDrawPolygons 4 ' contains list of polygon vertices 5 Private points As New ArrayList() 6 7 ' initialize default pen and brush 8 Private pen As New Pen(Color.DarkBlue) 9 Private brush As New SolidBrush(Color.DarkBlue) 10 11 ' draw panel mouse down event handler 12 Private Sub pnlDraw_MouseDown(ByVal sender As Object, _ 13 ByVal e As System.Windows.Forms.MouseEventArgs) _ 14 Handles pnlDraw.MouseDown 15 ' add mouse position to vertex list 16 points.Add(New Point(e.X, e.Y)) 17 pnlDraw.Invalidate() ' refresh panel 18 End Sub ' FrmDrawPolygons_MouseDown 19 20 ' draw panel Paint event handler 21 Private Sub pnlDraw_Paint(ByVal sender As Object, _ 22 ByVal e As System.Windows.Forms.PaintEventArgs) _ 23 Handles pnlDraw.Paint 24 ' get graphics object for panel 25 Dim graphicsObject As Graphics = e.Graphics 26 27 ' if arraylist has 2 or more points, display shape 28 If points.Count > 1 Then 29 ' get array for use in drawing functions 30 Dim pointArray As Point() = _ 31 CType(points.ToArray(points(0).GetType()), Point()) 32 33 If radLineOption.Checked Then 34 graphicsObject.DrawLines(pen, pointArray) 35 ElseIf radPolygonOption.Checked Then 36 graphicsObject.DrawPolygon(pen, pointArray) 37 ElseIf radFilledPolygonOption.Checked Then 38 graphicsObject.FillPolygon(brush, pointArray) 39 End If 40 End If 41 End Sub ' pnlDraw_Paint 42 43 ' clear all points in the polygon 44 Private Sub btnClear_Click(ByVal sender As System.Object, _ 45 ByVal e As System.EventArgs) Handles btnClear.Click 46 points.Clear() ' remove points 47 pnlDraw.Invalidate() ' refresh panel 48 End Sub ' btnClear_Click 49 50 ' change pen and brush colors 51 Private Sub btnColor_Click(ByVal sender As System.Object, _ 52 ByVal e As System.EventArgs) Handles btnColor.Click 53 ' create new color dialog 54 Dim dialogColor As New ColorDialog() 55 56 ' show dialog and obtain result 57 Dim result As DialogResult = dialogColor.ShowDialog() 58 59 ' return if user cancels 60 If result = Windows.Forms.DialogResult.Cancel Then 61 Return 62 End If 63 64 pen.Color = dialogColor.Color ' set pen to color 65 brush.Color = dialogColor.Color ' set brush 66 pnlDraw.Invalidate() ' refresh panel 67 End Sub ' btnColor_Click 68 69 ' cause Paint event 70 Private Sub radLineOption_CheckedChanged(_ 71 ByVal sender As System.Object, ByVal e As System.EventArgs) _ 72 Handles radLineOption.CheckedChanged 73 74 pnlDraw.Invalidate() ' refresh panel 75 End Sub ' radLineOption 76 77 ' cause Paint event 78 Private Sub radPolygonOption_CheckedChanged(_ 79 ByVal sender As System.Object, ByVal e As System.EventArgs) _ 80 Handles radPolygonOption.CheckedChanged 81 82 pnlDraw.Invalidate() ' refresh panel 83 End Sub ' radPolygonOption 84 85 ' cause Paint event 86 Private Sub radFilledPolygonOption_CheckedChanged(_ 87 ByVal sender As System.Object, ByVal e As System.EventArgs) _ 88 Handles radFilledPolygonOption.CheckedChanged 89 90 pnlDraw.Invalidate() ' refresh panel 91 End Sub ' radFilledPolygonOption 92 End Class ' FrmDrawPolygons | To allow the user to specify a variable number of points, line 5 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 89 declare the Pen and Brush used to color our shapes. The MouseDown event handler (lines 1218) for pnlDraw stores mouse-click locations in points with ArrayList method Add (line 16). The event handler then calls method Invalidate of pnlDraw (line 17) to ensure that the panel refreshes to accommodate the new point. Method pnlDraw_Paint (lines 2141) handles the Panel's Paint event. It obtains the Panel's Graphics object (line 25) and, if the ArrayList points contains two or more Points (line 28), displays the polygon with the method the user selected via the GUI radio buttons (lines 3339). In lines 3031, 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. Note that line 31 uses Visual Basic's CType function to convert the Object array returned by ToArray into an array of type Point. This is required because implicit conversions are not allowed if Option Strict is turned on (as is the case in this chapter). A conversion performed with the CType function is also known as a cast operation. Method btnClear_Click (lines 4448) handles the Clear button's Click event by calling ArrayList method Clear (causing the old list to be erased) and refreshing the display. Event handler btnColor_Click (5167) allows the user to select a new drawing color with a ColorDialog, using the techniques demonstrated in Fig. 17.7. Lines 7091 define the event handlers for each radio button's CheckedChanged event. Each method invalidates pnlDraw to ensure that the panel is repainted to reflect the selected shape type. |