Drawing in Visual Basic

A brush is similar to a pen except it does not have a width. Change the code as shown below:

Public Class Form1
Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
Dim MyPen As New Pen(Color.Black, 3) 'Black, width of 3
Dim MyBrush As New SolidBrush(Color.LawnGreen)
Me.CreateGraphics.FillEllipse(MyBrush, e.X - 15, e.Y - 15, 30, 30)
Me.CreateGraphics.DrawEllipse(MyPen, e.X - 15, e.Y - 15, 30, 30)
End Sub
End Class
a solid circle
Because we first draw the circle with the brush, then draw a circle on top of it with the pen, we get a solid green circle with a black outline. We have also modified the code so that the center of the circle is at the point clicked.

experiment
Experiment
: Use other colors, and other size ellipses. See if you can duplicate the program in the illustration. (You need two brushes.)


Brush vs. Pen

rectangles
The pen is used with Draw methods and the brush is used with Fill methods. Using the code above as an example, draw rectangles instead of circles:

Public Class Form1
    Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
        Dim MyPen As New Pen(Color.Black, 3) 'Black, width of 3
        Dim MyBrush As New SolidBrush(Color.LawnGreen)
        Me.CreateGraphics.FillRectangle(MyBrush, e.X - 15, e.Y - 15, 30, 30)
        Me.CreateGraphics.DrawRectangle(MyPen, e.X - 15, e.Y - 15, 30, 30)
    End Sub
End Class

NEXT: Group Boxes