Solved

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

Posted on 2009-04-27
792 Views
1.) How do you draw a polygon
2.) How do you fill a polygon
0
Question by:NerdsOfTech

LVL 85

Expert Comment

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

LVL 19

Author Comment

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

Thanks!
0

LVL 19

Author Comment

On a blank Form1
0

LVL 85

Expert Comment

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

LVL 85

Accepted Solution

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

LVL 19

Author Closing Comment

Thank you for the working example! Works great!
0

## Featured Post

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…