• Status: Solved
• Priority: Medium
• Security: Public
• Views: 824

# How do I create a filled polygon shape in Visual Basic 2008 Express?

1.) How do you draw a polygon
2.) How do you fill a polygon
0
NerdsOfTech
• 3
• 3
1 Solution

Middle School Assistant TeacherCommented:
Create a GraphicsPath instance and then use the Add() functions to add your shapes.  Then you can use the DrawGraphics() and/or FillGraphics() methods of your Graphics instance to draw it.

See: http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.graphicspath.aspx
and: http://msdn.microsoft.com/en-us/library/kz53wdz9.aspx
0

Technology ScientistAuthor Commented:
I'll check that out. Can you show me a working example of a filled pentagon for instance?

Thanks!
0

Technology ScientistAuthor Commented:
On a blank Form1
0

Middle School Assistant TeacherCommented:
I used the formula posted by Soroban here to calculate the points of the polygon:
http://www.mathhelpforum.com/math-help/pre-calculus/3076-how-draw-pentagon.html

Then I used the simple code below using Graphics.FillPolygon() and Graphics.DrawPolygon() to display it:

``````Public Class Form1

Private radius As Integer = 50
Private center As New Point(100, 100)
Private points As New List(Of Point)
Private Const PI As Double = 3.1415926

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim x, y As Integer
For n As Integer = 0 To 4
x = center.X + radius * Math.Sin(2 * PI / 5 * n)
y = center.Y - radius * Math.Cos(2 * PI / 5 * n)
Next
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.FillPolygon(Brushes.Red, points.ToArray)
e.Graphics.DrawPolygon(Pens.Black, points.ToArray)
End Sub

End Class
``````
DrawPolygon.jpg
0

Middle School Assistant TeacherCommented:
Here is the same thing but using a GraphicsPath.  (Note that you could add multiple shapes to the GraphicsPath and then draw them all at once....but they would they would all have to be the same color.)
``````Imports System.Drawing.Drawing2D
Public Class Form1

Private radius As Integer = 50
Private center As New Point(100, 100)
Private PentagonPath As New GraphicsPath
Private points As New List(Of Point)
Private Const PI As Double = 3.1415926

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim x, y As Integer
For n As Integer = 0 To 4
x = center.X + radius * Math.Sin(2 * PI / 5 * n)
y = center.Y - radius * Math.Cos(2 * PI / 5 * n)
Next
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.FillPath(Brushes.Red, PentagonPath)
e.Graphics.DrawPath(Pens.Black, PentagonPath)
End Sub

End Class
``````
0

Technology ScientistAuthor Commented:
Thank you for the working example! Works great!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.