Draw Ploygon : Ploygon « 2D « VB.Net






Draw Ploygon

Draw Ploygon
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.Windows.Forms
Imports System.Drawing.Text

Public Class MainClass

   Shared Sub Main()
        Dim myform As Form = New FrmPolygon()
        Application.Run(myform)
   End Sub ' Main

End Class


Public Class FrmPolygon
   Inherits System.Windows.Forms.Form

   ' polygon type options
   Friend WithEvents filledPolygonRadio As RadioButton
   Friend WithEvents lineRadio As RadioButton
   Friend WithEvents polygonRadio As RadioButton

   ' command buttons
   Friend WithEvents cmdClear As Button
   Friend WithEvents cmdNewColor As Button

   Friend WithEvents drawWindow As Panel

   Friend WithEvents typeGroup As GroupBox


#Region " Windows Form Designer generated code "

   Public Sub New()
      MyBase.New()

      'This call is required by the Windows Form Designer.
      InitializeComponent()

      'Add any initialization after the InitializeComponent() call

   End Sub

   'Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
      If disposing Then
         If Not (components Is Nothing) Then
            components.Dispose()
         End If
      End If
      MyBase.Dispose(disposing)
   End Sub

   'Required by the Windows Form Designer
   Private components As System.ComponentModel.Container

   'NOTE: The following procedure is required by the Windows Form Designer
   'It can be modified using the Windows Form Designer.  
   'Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
      Me.typeGroup = New System.Windows.Forms.GroupBox()
      Me.filledPolygonRadio = New System.Windows.Forms.RadioButton()
      Me.lineRadio = New System.Windows.Forms.RadioButton()
      Me.polygonRadio = New System.Windows.Forms.RadioButton()
      Me.drawWindow = New System.Windows.Forms.Panel()
      Me.cmdClear = New System.Windows.Forms.Button()
      Me.cmdNewColor = New System.Windows.Forms.Button()
      Me.typeGroup.SuspendLayout()
      Me.SuspendLayout()
      '
      'typeGroup
      '
      Me.typeGroup.Controls.AddRange(New System.Windows.Forms.Control() {Me.filledPolygonRadio, Me.lineRadio, Me.polygonRadio})
      Me.typeGroup.Location = New System.Drawing.Point(232, 8)
      Me.typeGroup.Name = "typeGroup"
      Me.typeGroup.Size = New System.Drawing.Size(128, 104)
      Me.typeGroup.TabIndex = 0
      Me.typeGroup.TabStop = False
      Me.typeGroup.Text = "Select Type"
      '
      'filledPolygonRadio
      '
      Me.filledPolygonRadio.Location = New System.Drawing.Point(8, 80)
      Me.filledPolygonRadio.Name = "filledPolygonRadio"
      Me.filledPolygonRadio.Size = New System.Drawing.Size(112, 16)
      Me.filledPolygonRadio.TabIndex = 2
      Me.filledPolygonRadio.Text = "Filled Polygon"
      '
      'lineRadio
      '
      Me.lineRadio.Location = New System.Drawing.Point(8, 24)
      Me.lineRadio.Name = "lineRadio"
      Me.lineRadio.Size = New System.Drawing.Size(112, 16)
      Me.lineRadio.TabIndex = 1
      Me.lineRadio.Text = "Lines"
      '
      'polygonRadio
      '
      Me.polygonRadio.Location = New System.Drawing.Point(8, 48)
      Me.polygonRadio.Name = "polygonRadio"
      Me.polygonRadio.Size = New System.Drawing.Size(112, 24)
      Me.polygonRadio.TabIndex = 0
      Me.polygonRadio.Text = "Polygon"
      '
      'drawWindow
      '
      Me.drawWindow.BackColor = System.Drawing.SystemColors.Window
      Me.drawWindow.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
      Me.drawWindow.Name = "drawWindow"
      Me.drawWindow.Size = New System.Drawing.Size(232, 264)
      Me.drawWindow.TabIndex = 1
      '
      'cmdClear
      '
      Me.cmdClear.Location = New System.Drawing.Point(240, 160)
      Me.cmdClear.Name = "cmdClear"
      Me.cmdClear.Size = New System.Drawing.Size(112, 24)
      Me.cmdClear.TabIndex = 2
      Me.cmdClear.Text = "Clear"
      '
      'cmdNewColor
      '
      Me.cmdNewColor.Location = New System.Drawing.Point(240, 192)
      Me.cmdNewColor.Name = "cmdNewColor"
      Me.cmdNewColor.Size = New System.Drawing.Size(112, 24)
      Me.cmdNewColor.TabIndex = 3
      Me.cmdNewColor.Text = "Change Color"
      '
      'FrmPolygon
      '
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(360, 269)
      Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cmdNewColor, Me.cmdClear, Me.drawWindow, Me.typeGroup})
      Me.Name = "FrmPolygon"
      Me.Text = "Drawing Polygons"
      Me.typeGroup.ResumeLayout(False)
      Me.ResumeLayout(False)

   End Sub

#End Region

   Private mPoints As ArrayList = New ArrayList()

   Dim mPen As Pen = New Pen(Color.DarkBlue)
   Dim mBrush As SolidBrush = New SolidBrush(Color.DarkBlue)

   Private Sub drawWindow_MouseDown(ByVal sender _
      As Object, ByVal e As _
      System.Windows.Forms.MouseEventArgs) _
      Handles drawWindow.MouseDown

      mPoints.Add(New Point(e.X, e.Y))
      drawWindow.Invalidate() ' refresh panel
   End Sub 

   Private Sub drawWindow_Paint(ByVal sender As Object, _
      ByVal e As System.Windows.Forms.PaintEventArgs) _
      Handles drawWindow.Paint

      Dim graphicsObject As Graphics = e.Graphics

      If mPoints.Count > 1 Then

         Dim pointArray() As Point = _
            mPoints.ToArray(mPoints(0).GetType())

         If polygonRadio.Checked Then 
            graphicsObject.DrawPolygon(mPen, pointArray)

         ElseIf lineRadio.Checked Then
            graphicsObject.DrawLines(mPen, pointArray)

         ElseIf filledPolygonRadio.Checked Then 
            
            graphicsObject.FillPolygon(mBrush, pointArray)
         End If

      End If

   End Sub 

   Private Sub cmdClear_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles cmdClear.Click

      mPoints = New ArrayList()
      drawWindow.Invalidate()
   End Sub

   Private Sub polygonRadio_CheckedChanged(ByVal sender As _
      System.Object, ByVal e As System.EventArgs) _
      Handles polygonRadio.CheckedChanged

      drawWindow.Invalidate()
   End Sub
   Private Sub lineRadio_CheckedChanged(ByVal sender As _
      System.Object, ByVal e As System.EventArgs) _
      Handles lineRadio.CheckedChanged

      drawWindow.Invalidate()
   End Sub

   Private Sub filledPolygonRadio_CheckedChanged(ByVal sender _
      As System.Object, ByVal e As System.EventArgs) _
      Handles filledPolygonRadio.CheckedChanged

      drawWindow.Invalidate() 
   End Sub 

   Private Sub cmdNewColor_Click(ByVal sender As _
      System.Object, ByVal e As System.EventArgs) _
      Handles cmdNewColor.Click

      Dim colorBox As ColorDialog = New ColorDialog()

      Dim result As DialogResult = colorBox.ShowDialog()

      If result = DialogResult.Cancel Then
         Return
      End If

      mPen.Color = colorBox.Color 
      mBrush.Color = colorBox.Color 
      drawWindow.Invalidate() 
   End Sub 

End Class
           
       








Related examples in the same category