Drawing in Visual Basic

Some of the drawing methods require an array of points. Although we will discuss arrays in more detail later in this text, we will give a very brief explanation here so that we can draw a few other shapes. An array is simply a list. In order to draw a triangle, we will declare 3 points, and then make an array (list) of those three points.

triangle using array of points

Public Class Form1
    Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
        Dim MyPen As New Pen(Color.Green, 3)
        Dim Pt1 As New Point(e.X - 15, e.Y)
        Dim Pt2 As New Point(e.X + 15, e.Y)
        Dim Pt3 As New Point(e.X, e.Y + 15)
        Dim Pts As Point() = {Pt1, Pt2, Pt3}
        Me.CreateGraphics.DrawPolygon(MyPen, Pts)
    End Sub
End Class

Bezier Curves

bezier curveA Bezier curve is defined by four control points Pt1, Pt2, Pt3, and Pt4. It starts at Pt1 heading toward Pt2, and ends at Pt4 coming from the direction of Pt3. It may help to picture a Bezier curve as a line from Pt1 to Pt4 that is pulled like rubber band towards Pt2 and Pt3.


Public Class Form1
    Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
        Dim MyPen As New Pen(Color.Green, 3)
        Dim Pt1 As New Point(e.X - 50, e.Y - 50)
        Dim Pt2 As New Point(e.X - 50, e.Y + 50)
        Dim Pt3 As New Point(e.X + 50, e.Y - 50)
        Dim Pt4 As New Point(e.X + 50, e.Y + 50)
        Me.CreateGraphics.DrawBezier(MyPen, Pt1, Pt2, Pt3, Pt4)
    End Sub
End Class
Bezier 2This curve was created by changing Pt2 and Pt3:
Dim Pt2 As New Point(e.X - 150, e.Y + 150)
Dim Pt3 As New Point(e.X + 150, e.Y - 150)

NEXT: Drawing Strings: The Font Class