Lesson 25    |  Home  | Lesson 27 

VB2010 Lesson 26: Managing Graphics-Filling Shapes with Color

In previous 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. 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)

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 intellisense will display the colors and enter the period after the Color key word.

26.1 Drawing and Filling a Rectangle

The syntax to fill a rectangle with the color 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 color defined by the brush object is:

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

The complete code is shown in the example below:

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 below:

 

26.3 Drawing and Filling a Polygon

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

myGraphics.FillPolygon(myBrush, myPoints)

The complete code is shown in the example below:

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 color defined by the brush object is:

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

The complete code is shown in the example below:

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:

 

 Bookmark and Share

Copyright  2010  Dr.Liew Voon Kiong . All rights reserved |Contact: vbtutor facebook page [Privacy Policy]

 

 

 

This Web Page Created with PageBreeze Free Website Builder