VB2019 VB2017 VB2015 VB2013 VB2012 VB2010 VB2008 VB6 VB Sample Codes 中文VB About Us

Lesson 31 Coloring Shapes


In preceding Lessons, we have learned how to draw rectangle, ellipse, circle, polygon and pie with outlines only. In this Lesson, we will show you how to fill the shapes with color, or simply solid shapes in Visual Basic 2015. Three methods that are used to fill shapes are FillRectangle, FillEllipse, FillPolygon and FillPie.In order to fill the above shapes with color, we need to create the Brush object using the following syntax:

myBrush = New SolidBrush(Color.myColor)

*myColor can be any color such as red, IntelliSense blue, yellow and more. You don’t have to worry about the names of the colors IntelliSensewill display the colors and enter the period after the Color keyword.

31.1 Drawing and Filling a Rectangle with Color

In Visual Basic 2015, the syntax to fill a rectangle with the color defined by the brush object is:

myGraphics.FillRectangle (myBrush, 0, 0, 150, 150)

Example 31.1

Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click
Dim myPen As Pen
Dim myBrush As Brush
Dim myGraphics As Graphics = Me.CreateGraphicsmyPen = New Pen(Drawing.Color.Blue, 5)
 myBrush = New SolidBrush(Color.Coral)
 myGraphics.DrawRectangle(myPen, 65, 50, 150, 150)
 myGraphics.FillRectangle(myBrush, 65, 50, 150, 150)
End Sub

The output is as shown in Figure 31.1

VB2013_figure30.1
Figure 31.1

*Note that if you omit the line myGraphics.DrawRectangle(myPen, 65, 50, 150, 150), you will get a solid rectangle without outline, as shown in in Figure 31.2

VB2013_figure30.2
Figure 31.2

31.2 Drawing and Filling an Ellipse with Color

The syntax to fill a ellipse with the color defined by the brush object is:

myGraphics.FillEllipse (myBrush, 0, 0, 150, 150)

Example 31.2

Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click
Dim myPen As Pen
Dim myBrush As Brush
Dim myGraphics As Graphics = Me.CreateGraphicsmyPen = New Pen(Drawing.Color.Blue, 5)
 myBrush = New SolidBrush(Color.Coral)
 myGraphics.DrawEllipse(myPen, 50, 50, 180, 100)
 myGraphics.FillEllipse(myBrush, 50, 50, 180, 100)

End Sub

The output interface is as shown in Figure 31.3

VB2013_figure30.3
 Figure 31.3

*If you omit the line myGraphics.DrawEllipse(myPen, 50, 50, 180, 100), you will get a solid ellipse without outline.

31.3 Drawing and Filling a Polygon with Color

The syntax to fill a polygon with the color defined by the brush object is:

myGraphics.FillPolygon(myBrush, myPoints)

Example 31.3

Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click
Dim myPen As Pen
Dim myBrush As Brush
Dim A As New Point(70, 10)
Dim B As New Point(170, 50)
Dim C As New Point(200, 150)
Dim D As New Point(140, 200)
Dim myPoints As Point() = {A, B, C, D}

 myPen = New Pen(Drawing.Color.Blue, 5)
 myBrush = New SolidBrush(Color.Coral)
Dim myGraphics As Graphics = Me.CreateGraphics
 myGraphics.DrawPolygon(myPen, myPoints)
 myGraphics.FillPolygon(myBrush, myPoints)

End Sub

The output interface is as shown in Figure 31.4

VB2013_figure30.4
Figure 31.4

* if you omit myGraphics the line DrawPolygon(myPen, myPoints),  you will get a polygon without outline

 

31.4 Drawing and Filling a Pie

The syntax to fill a pie with the color defined by the brush object is:

myGraphics.FillPie(myBrush, X, Y, width, height, StartAngle, SweepAngle)

Example 31.4

Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click
Dim myPen As Pen
Dim myBrush As Brush
Dim myGraphics As Graphics = Me.CreateGraphics

 myPen = New Pen(Drawing.Color.Blue, 5)
 myBrush = New SolidBrush(Color.Coral)
 myGraphics.DrawPie(myPen, 30, 40, 150, 150, 0, 60)
 myGraphics.FillPie(myBrush, 30, 40, 150, 150, 0, 60)

End Sub

The output is as shown in Figure 31.5

VB2013_figure30.5
Figure 31.5


❮ Previous Lesson Next Lesson ❯


Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy