[Lesson 25] << [CONTENTS] >> [Lesson 27]
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 below:
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 following figure:
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 below:
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:
[Lesson 25] << [CONTENTS] >> [Lesson 27]