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

Lesson 26 : Filling Shapes with Color


In this Lesson, we will illustrate how to fill the shapes with colors. In Visual Basic 2010, three methods are used to fill shapes are FillRectangle, FillEllipse, FillPolygon and FillPie.In order to fill the aforementioned shapes with color, we have to create the Brush object using the following syntax:

myBrush = New SolidBrush(Color.myColor)

Where myColor can be any color such as red, blue, yellow and more. You don’t have to worry about the names of the colors because the intelligence will display the colours and enter the period after the Color key word.

26.1 Drawing and Filling a Rectangle

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

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

The complete code is shown in the example below:

Example 26.1

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.DrawRectangle(myPen, 0, 0, 150, 150)
 myGraphics.FillRectangle(myBrush, 0, 0, 150, 150)

The Output is shown in Figure 26.1

Figure 26.1

26.2 Drawing and Filling an Ellipse

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

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

The complete code is shown in Example 26.2

Example 26.2

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.DrawEllipse(myPen, 0, 0, 150, 150)
 myGraphics.Ellipse(myBrush, 0, 0, 150, 150)

The output is shown in the Figure 26.2

Figure 26.2

26.3 Drawing and Filling a Polygon

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

myGraphics.FillPolygon(myBrush, myPoints)

The complete code is shown in the Example 26.3

Example 26.3

Dim myPen As Pen
Dim myBrush As Brush
Dim A As New Point(10, 10)
Dim B As New Point(100, 50)
Dim C As New Point(120, 150)
Dim D As New Point(60, 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)

Running the code produces the image as shown in Figure 26.3

Figure 26.3

26.4 Drawing and Filling a Pie

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

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

The complete code is shown in the following Example 26.4

Example 26.4

Dim myPen As Pen
Dim myBrush As Brush
 myPen = New Pen(Drawing.Color.Blue, 5)
 myBrush = New SolidBrush(Color.Coral)
Dim myGraphics As Graphics = Me.CreateGraphics
 myGraphics.DrawPie(myPen, 30, 40, 150, 150, 0, 60)
 myGraphics.FillPie(myBrush, 30, 40, 150, 150, 0, 60)

The output is shown below:

Figure 26.4


❮ Previous Lesson Next Lesson ❯


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