JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Filling Figures with Brushes

You use brushes to fill in graphics figures. (See the previous topic and the discussion in the In Depth section of this chapter.) To illustrate, take a look at the FillGraphics example on the CD-ROM, which you can see at work in Figure 13.2. This example draws four ellipses and fills them in with the FillEllipse method using various brushes-LinearGradientBrush, HatchBrush, SolidBrush, and TextureBrush. To use these classes, you import the System.Drawing.Drawing2D namespace. Here's what this example looks like in code; note that the texture brush uses an image to fill a figure. In this case, I'm using the greenstone.bmp image that comes with Windows 2000:

Imports System.Drawing.Drawing2D
Public Class Form1
    Inherits System.Windows.Forms.Form

    'Windows Form Designer generated code

    Private Sub Form1_Paint(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.PaintEventArgs) _
        Handles MyBase.Paint
        Dim g As Graphics
        Dim r As Rectangle
        g = Me.CreateGraphics()

        r = New Rectangle(20, 20, 60, 60)
        Dim lb As New LinearGradientBrush(_
           r, Color.Red, Color.Yellow, LinearGradientMode.Horizontal)
        g.FillEllipse(lb, r)

        r = New Rectangle(20, 100, 60, 60)
        Dim hb As New HatchBrush(HatchStyle.DarkDownwardDiagonal, _
        g.FillEllipse(hb, r)

        r = New Rectangle(100, 20, 60, 60)
        Dim sb As New SolidBrush(Color.Coral)
        g.FillEllipse(sb, r)

        r = New Rectangle(100, 100, 60, 60)
        Dim tb As New TextureBrush(Bitmap.FromFile("greenstone.bmp"))
        g.FillEllipse(tb, r)
    End Sub
End Class

To use a Pen, you use the DrawXXX method of the Graphics class, and to fill the figure in using a brush, you use the FillXXX methods. For more on setting the colors of brushes, see "Specifying Drawing Colors," and to see how to use pre-defined brushes, see "Using the Pens and Brushes Classes," both in this chapter.

Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor