Lesson 31: Coloring Shapes in VB2022

Master color application techniques to create vibrant graphics and data visualizations

Key Takeaway

Coloring shapes brings your graphics to life. Master SolidBrush techniques to create vibrant visualizations and enhance your VB2022 applications.

Welcome to Lesson 31 of our Visual Basic 2022 Tutorial! Building on our graphics knowledge from previous lessons, we'll now explore how to fill shapes with colors using VB2022's powerful GDI+ tools. These techniques are essential for creating visually appealing interfaces, data visualizations, and custom graphics.

31.1 Understanding Brushes

To fill shapes with color in VB2022, we use Brush objects. The most common brush is the SolidBrush, which fills shapes with a single solid color.

1. Create Brush

Instantiate a SolidBrush with your desired color

2. Select Shape

Choose the appropriate Fill method for your shape

3. Apply Color

Call the Fill method with your brush and shape parameters

Pro Tip: Brush Management

Always dispose of Brush objects when you're done with them to free up resources.

Example 31.1: Creating a SolidBrush

This example demonstrates how to create a SolidBrush:

CreateBrush.vb
Private Sub CreateBrushExample()
    ' Create a blue SolidBrush
    Dim blueBrush As New SolidBrush(Color.Blue)
    
    ' Create a custom color brush using RGB values
    Dim customColor As Color = Color.FromArgb(255, 200, 100, 50)
    Dim customBrush As New SolidBrush(customColor)
    
    ' Use the brushes...
    
    ' Dispose of brushes when done
    blueBrush.Dispose()
    customBrush.Dispose()
End Sub

31.2 Filling Rectangles

Rectangles can be filled using the FillRectangle method with a brush:

Example 31.2: Filled Rectangle

Draw and fill a rectangle with a coral color:

FillRectangle.vb
Private Sub BtnDrawRectangle_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click
    ' Create graphics object
    Dim g As Graphics = Me.CreateGraphics()
    
    ' Create pen and brush
    Dim myPen As New Pen(Color.Blue, 5)
    Dim myBrush As New SolidBrush(Color.Coral)
    
    ' Draw rectangle outline
    g.DrawRectangle(myPen, 65, 50, 150, 150)
    
    ' Fill rectangle with color
    g.FillRectangle(myBrush, 65, 50, 150, 150)
    
    ' Clean up resources
    myPen.Dispose()
    myBrush.Dispose()
    g.Dispose()
End Sub
Filled Rectangle

Combined outline and fill creates a bordered colored rectangle

Solid Rectangle

Omitting DrawRectangle creates a solid shape without outline

31.3 Filling Ellipses and Circles

Ellipses and circles can be filled using the FillEllipse method:

Example 31.3: Filled Ellipse

Draw and fill an ellipse with a custom color:

FillEllipse.vb
Private Sub BtnDrawEllipse_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click
    Dim g As Graphics = Me.CreateGraphics()
    
    ' Create a custom light blue color
    Dim lightBlue As Color = Color.FromArgb(200, 230, 255)
    
    Dim myPen As New Pen(Color.DarkBlue, 3)
    Dim myBrush As New SolidBrush(lightBlue)
    
    ' Draw ellipse outline
    g.DrawEllipse(myPen, 50, 50, 180, 100)
    
    ' Fill ellipse with color
    g.FillEllipse(myBrush, 50, 50, 180, 100)
    
    ' Clean up
    myPen.Dispose()
    myBrush.Dispose()
    g.Dispose()
End Sub
Filled Ellipse

Ellipse with dark blue outline and light blue fill

Filled Circle

Circle created by making width and height equal

31.4 Filling Polygons

Polygons are filled using the FillPolygon method with an array of points:

Example 31.4: Filled Polygon

Draw and fill a polygon with a gradient-like effect:

FillPolygon.vb
Private Sub BtnDrawPolygon_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click
    Dim g As Graphics = Me.CreateGraphics()
    
    ' Define polygon vertices
    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 E As New Point(50, 180)
    Dim myPoints As Point() = {A, B, C, D, E}
    
    Dim myPen As New Pen(Color.DarkBlue, 4)
    Dim myBrush As New SolidBrush(Color.LightSalmon)
    
    ' Draw polygon outline
    g.DrawPolygon(myPen, myPoints)
    
    ' Fill polygon with color
    g.FillPolygon(myBrush, myPoints)
    
    ' Clean up
    myPen.Dispose()
    myBrush.Dispose()
    g.Dispose()
End Sub
Filled Polygon

Pentagon with dark blue outline and light salmon fill

31.5 Filling Pie Segments

Pie segments can be filled using the FillPie method:

Example 31.5: Filled Pie Segment

Draw and fill a pie segment with violet color:

FillPie.vb
Private Sub BtnDrawPie_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click
    Dim g As Graphics = Me.CreateGraphics()
    
    ' Create pen and brush
    Dim myPen As New Pen(Color.Purple, 3)
    Dim myBrush As New SolidBrush(Color.Violet)
    
    ' Define bounding rectangle
    Dim rect As New Rectangle(50, 50, 150, 150)
    
    ' Draw pie outline
    g.DrawPie(myPen, rect, 0, 90)
    
    ' Fill pie segment with color
    g.FillPie(myBrush, rect, 0, 90)
    
    ' Clean up
    myPen.Dispose()
    myBrush.Dispose()
    g.Dispose()
End Sub
Filled Pie Segment

90-degree segment with violet fill and purple outline

Pie Chart

Complete pie chart with multiple colored segments

31.6 Advanced Brush Types

Beyond SolidBrush, VB2022 offers other brush types for more complex effects:

LinearGradientBrush

Smooth color transitions

RadialGradientBrush

Circular color transitions

');">

TextureBrush

Pattern-based fills

HatchBrush

Predefined patterns

Example 31.6: Gradient Brush

Create a rectangle with a gradient fill:

GradientBrush.vb
Private Sub BtnGradient_Click(sender As Object, e As EventArgs) Handles BtnGradient.Click
    Dim g As Graphics = Me.CreateGraphics()
    
    ' Define rectangle area
    Dim rect As New Rectangle(50, 50, 200, 150)
    
    ' Create gradient brush
    Dim gradientBrush As New Drawing2D.LinearGradientBrush(
        New Point(rect.Left, rect.Top),
        New Point(rect.Right, rect.Bottom),
        Color.Blue,
        Color.LightBlue
    )
    
    ' Fill rectangle with gradient
    g.FillRectangle(gradientBrush, rect)
    
    ' Clean up
    gradientBrush.Dispose()
    g.Dispose()
End Sub

Coloring Shapes Summary

Master these essential techniques for coloring shapes in VB2022:

Method Description Usage
FillRectangle Fills rectangular shapes g.FillRectangle(brush, x, y, width, height)
FillEllipse Fills elliptical shapes g.FillEllipse(brush, x, y, width, height)
FillPolygon Fills polygonal shapes g.FillPolygon(brush, points())
FillPie Fills pie segments g.FillPie(brush, rect, startAngle, sweepAngle)
SolidBrush Creates solid color fills New SolidBrush(color)

Best Practices

Always dispose of Graphics and Brush objects. Use the Paint event for persistent graphics.

Performance Tips

For complex graphics, create brushes once and reuse them when possible.

Advanced Features

Explore gradient brushes and texture brushes for more sophisticated effects.

Practical Exercises

Apply your shape coloring knowledge with these hands-on exercises:

Exercise 1: Color Palette

Create an application that displays a palette of 12 different colored squares using SolidBrush.

Exercise 2: Gradient Generator

Build a program that generates rectangles with linear gradients between two user-selected colors.

Exercise 3: Pie Chart Creator

Develop an interface that lets users input data values and generates a colored pie chart.

Exercise 4: Pattern Fills

Implement texture and hatch brushes to create shapes with patterned fills.

Exercise 5: Animated Color Transition

Create an animation where a shape gradually changes color using a Timer control.

Challenge Exercise: Color Picker Tool

Build a comprehensive color picker with RGB/HSL controls that previews colors on shapes.

Next Lesson

Learn how to create animations with the Timer control in Lesson 32: Using Timer.

Related Resources

VB6 Tutorial

Mastering VB6 Programming

Explore Tutorials

Visual Basic Examples

Practical VB code samples for real-world applications

View Examples

Excel VBA Tutorial

Learn how to automate Excel by creating VBA macros

Learn More