Visual Basic 2008 Tutorial

Lesson 27: Managing Graphics-Filling Shapes with Color

[Lesson 26]<<  [Home]



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.

27.1 Drawing and Filling a Rectangle

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

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

The complete code is shown in the example below:

Example 27.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 image is shown below:


27.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 27.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 image is shown below:


27.3 Drawing and Filling a Polygon

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

 nbsp; nbsp; nbsp;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:


27.4 Drawing and Filling a Pie

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

 nbsp; nbsp; nbsp;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

   nbsp;  myGraphics.DrawPie(myPen, 30, 40, 150, 150, 0, 60)

The Output Image



  Bookmark and Share


[Lesson 26]<<  [Home]

Copyright® Dr.Liew Voon Kiong  Contact: admin@vbtutor.net Privacy Policy]