![]() |
Introduction to Brushes |
|
Fundamentals of Brushes |
|
Introduction |
|
In the previous lesson, we drew two types of figures: line-based and closed shapes. These figures required a pen to show their shape. The particularity with closed shapes is that they can be filled, with a color, a picture, or a pattern. A brush is an object that holds a color, a picture, or a drawing pattern and that is used to fill the interior of a closed shape. This definition also means that there are various types of brushes with different goals. To meet these goals, the .NET Framework provides support for brushes in various namespaces with different classes. The parent of all brushes is the Brush class defined in the System.Drawing namespace. |
Because the main job of a brush is to fill a closed shape, the Graphics class provides a method that corresponds to each of the closed shapes we reviewed to draw in the previous lesson in order to fill it. The methods are:
To fill out a shape, call one of these methods, pass it a brush value, followed by the location and dimensions of the shape. For example, if you want to draw a rectangle and fill it with a brush, you would use code similar to: Imports System.Drawing
Imports System.Windows.Forms
Module Exercise
Public Class Starter
Inherits Form
Dim components As System.ComponentModel.Container
Public Sub New()
InitializeComponent()
End Sub
Public Sub InitializeComponent()
End Sub
Private Sub FormPaint(ByVal sender As Object, _
ByVal e As PaintEventArgs) _
Handles MyBase.Paint
e.Graphics.FillRectangle(SomeBrush, 20, 20, 200, 160)
End Sub
End Class
Function Main() As Integer
Dim frmStart As Starter = New Starter
Application.Run(frmStart)
Return 0
End Function
End Module
Over all, there are four types of brushes.
Like a pen, the primary characteristic of a brush is its color. To help you create a simple brush, the System.Drawing namespace provides the static sealed Brushes class. The only feature this class provides is the ability to specify a color to use on a brush. As a static class, you never have to instantiate it. To create a simple brush whose only information is provided by its color, call the Brushes class and access a color by qualifying it with the name of the class. Each color is provided by its name as a property. Here is an example of using the class: Imports System.Drawing
Imports System.Windows.Forms
Module Exercise
Public Class Starter
Inherits Form
Dim components As System.ComponentModel.Container
Public Sub New()
InitializeComponent()
End Sub
Public Sub InitializeComponent()
End Sub
Private Sub FormPaint(ByVal sender As Object, _
ByVal e As PaintEventArgs) _
Handles MyBase.Paint
Dim pt As Point() = { _
New Point(10, 22), _
New Point(188, 246), _
New Point(280, 192), _
New Point(250, 48)}
e.Graphics.FillClosedCurve(Brushes.BlanchedAlmond, pt)
e.Graphics.DrawClosedCurve(Pens.Blue, pt)
End Sub
End Class
Function Main() As Integer
Dim frmStart As Starter = New Starter
Application.Run(frmStart)
Return 0
End Function
End Module
This would produce:
The simplest type of brush is referred to as solid. This type of brush is simply equipped with a color and it is used to fill a shape with it. To get a solid brush, you use the SolidBrush class defined in the System.Drawing namespace. It has only one constructor declared with the following syntax: Public Sub New(color As Color) The color passed as argument must be a valid definition of a Color. Here is an example: Private Sub FormPaint(ByVal sender As Object, _
ByVal e As PaintEventArgs) _
Handles MyBase.Paint
Dim brushBlue As SolidBrush = New SolidBrush(Color.Blue)
e.Graphics.FillRectangle(brushBlue, 20, 20, 200, 160)
End Sub
This would produce:
If you plan to use different colors to fill different shapes, you don't have to create a new brush for each shape. At any time, before re-using the same brush previously defined, you can simply change its Color. For this reason, the SolidBrush class is equipped with the Color property. Here is an example of using it: Private Sub FormPaint(ByVal sender As Object, _
ByVal e As PaintEventArgs) _
Handles MyBase.Paint
Dim colorizer As SolidBrush = New SolidBrush(Color.Lime)
e.Graphics.FillRectangle(colorizer, 10, 10, 120, 120)
colorizer.Color = Color.Salmon
e.Graphics.FillRectangle(colorizer, 140, 10, 120, 120)
colorizer.Color = Color.Aqua
e.Graphics.FillRectangle(colorizer, 10, 140, 120, 120)
colorizer.Color = Color.Navy
e.Graphics.FillRectangle(colorizer, 140, 140, 120, 120)
End Sub
This would produce:
Like most objects used in graphics programming, a brush consumes the computer resources. Therefore, after using it, you can free the resources it was using by calling the Dispose() method. Here is an example: Private Sub FormPaint(ByVal sender As Object, _
ByVal e As PaintEventArgs) _
Handles MyBase.Paint
Dim colorizer As SolidBrush = New SolidBrush(Color.Lime)
e.Graphics.FillRectangle(colorizer, 10, 10, 120, 120)
colorizer.Color = Color.Salmon
e.Graphics.FillRectangle(colorizer, 140, 10, 120, 120)
colorizer.Color = Color.Aqua
e.Graphics.FillRectangle(colorizer, 10, 140, 120, 120)
colorizer.Color = Color.Navy
e.Graphics.FillRectangle(colorizer, 140, 140, 120, 120)
colorizer.Dispose()
End Sub
A hatch brush relies on a drawn or designed pattern to set its filling type. To support hatch brushes, the .NET Framework provides the patterns you can use as part of the brush. These pre-designed patterns are referred to as hatch styles. This means that when you use a hatch brush, you must specify the type of pattern you want to use, through one of the available hatch styles. To make the filled area more interesting, you also specify the color to use when drawing the pattern. To get a hatch brush, you use the HatchBrush class. One of its constructors has the following syntaxes: Public Sub New(hatchstyle As HatchStyle, foreColor As Color)
The foreColor argument is the color that will be used to draw the pattern. The style argument is the hatch style you want to apply. Some of the available styles are: Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Module Exercise
Public Class Starter
Inherits Form
Dim components As System.ComponentModel.Container
Public Sub New()
InitializeComponent()
End Sub
Public Sub InitializeComponent()
End Sub
Private Sub FormPaint(ByVal sender As Object, _
ByVal e As PaintEventArgs) _
Handles MyBase.Paint
Dim brushBackDiag As HatchBrush = _
New HatchBrush(HatchStyle.BackwardDiagonal, _
Color.FromArgb(0, 0, 255))
Dim brushCross As HatchBrush = _
New HatchBrush(HatchStyle.Cross, _
Color.FromArgb(200, 0, 0))
Dim brushDarkDown As HatchBrush = _
New HatchBrush(HatchStyle.DarkDownwardDiagonal, _
Color.Salmon)
Dim brushDarkHorz As HatchBrush = _
New HatchBrush(HatchStyle.DarkHorizontal, _
Color.Navy)
Dim brushDarkUpDiag As HatchBrush = _
New HatchBrush(HatchStyle.DarkUpwardDiagonal, _
Color.Pink)
Dim brushVertical As HatchBrush = _
New HatchBrush(HatchStyle.DarkVertical, _
Color.FromArgb(255, 0, 255))
Dim brushDashDnDiag As HatchBrush = _
New HatchBrush(HatchStyle.DashedDownwardDiagonal, _
Color.FromArgb(255, 128, 0))
Dim brushDashHorz As HatchBrush = _
New HatchBrush(HatchStyle.DashedHorizontal, _
Color.FromArgb(0, 128, 192))
Dim brushDashUpDiag As HatchBrush = _
New HatchBrush(HatchStyle.DashedUpwardDiagonal, _
Color.Green)
Dim brushDashVert As HatchBrush = _
New HatchBrush(HatchStyle.DashedVertical, _
Color.Firebrick)
Dim brushDiagBrisk As HatchBrush = _
New HatchBrush(HatchStyle.DiagonalBrick, _
Color.Fuchsia)
Dim brushDiagCross As HatchBrush = _
New HatchBrush(HatchStyle.DiagonalCross, _
Color.Moccasin)
Dim brushDivot As HatchBrush = _
New HatchBrush(HatchStyle.Divot, _
Color.Goldenrod)
Dim brushDotDiamond As HatchBrush = _
New HatchBrush(HatchStyle.DottedDiamond, _
Color.Gainsboro)
Dim brushDottedGrid As HatchBrush = _
New HatchBrush(HatchStyle.DottedGrid, _
Color.Khaki)
Dim brushForDiag As HatchBrush = _
New HatchBrush(HatchStyle.ForwardDiagonal, _
Color.Maroon)
Dim brushHorz As HatchBrush = _
New HatchBrush(HatchStyle.Horizontal, _
Color.Red)
Dim brushHorzBrick As HatchBrush = _
New HatchBrush(HatchStyle.HorizontalBrick, _
Color.SaddleBrown)
Dim brushLgChkBoard As HatchBrush = _
New HatchBrush(HatchStyle.LargeCheckerBoard, _
Color.RoyalBlue)
Dim brushLgConfetti As HatchBrush = _
New HatchBrush(HatchStyle.LargeConfetti, _
Color.MistyRose)
Dim brushLgGrid As HatchBrush = _
New HatchBrush(HatchStyle.LargeGrid, _
Color.Purple)
Dim brushLtDnDiag As HatchBrush = _
New HatchBrush(HatchStyle.LightDownwardDiagonal, _
Color.DarkCyan)
Dim brushLtHorz As HatchBrush = _
New HatchBrush(HatchStyle.LightHorizontal, _
Color.PowderBlue)
Dim brushUpDiag As HatchBrush = _
New HatchBrush(HatchStyle.LightUpwardDiagonal, _
Color.SeaGreen)
Dim brushLtVert As HatchBrush = _
New HatchBrush(HatchStyle.LightVertical, _
Color.Olive)
e.Graphics.FillRectangle(brushBackDiag, _
20, 20, 80, 60)
e.Graphics.FillRectangle(brushCross, _
120, 20, 80, 60)
e.Graphics.FillRectangle(brushDarkDown, _
220, 20, 80, 60)
e.Graphics.FillRectangle(brushDarkHorz, _
320, 20, 80, 60)
e.Graphics.FillRectangle(brushDarkUpDiag, _
420, 20, 80, 60)
e.Graphics.FillRectangle(brushVertical, _
20, 100, 80, 60)
e.Graphics.FillRectangle(brushDashDnDiag, _
120, 100, 80, 60)
e.Graphics.FillRectangle(brushDashHorz, _
220, 100, 80, 60)
e.Graphics.FillRectangle(brushDashUpDiag, _
320, 100, 80, 60)
e.Graphics.FillRectangle(brushDashVert, _
420, 100, 80, 60)
e.Graphics.FillRectangle(brushDashVert, _
20, 180, 80, 60)
e.Graphics.FillRectangle(brushDiagBrisk, _
120, 180, 80, 60)
e.Graphics.FillRectangle(brushDiagCross, _
220, 180, 80, 60)
e.Graphics.FillRectangle(brushDivot, _
320, 180, 80, 60)
e.Graphics.FillRectangle(brushDotDiamond, _
420, 180, 80, 60)
e.Graphics.FillRectangle(brushDottedGrid, _
20, 260, 80, 60)
e.Graphics.FillRectangle(brushForDiag, _
120, 260, 80, 60)
e.Graphics.FillRectangle(brushHorz, _
220, 260, 80, 60)
e.Graphics.FillRectangle(brushHorzBrick, _
320, 260, 80, 60)
e.Graphics.FillRectangle(brushLgChkBoard, _
420, 260, 80, 60)
e.Graphics.FillRectangle(brushLgGrid, _
20, 340, 80, 60)
e.Graphics.FillRectangle(brushLtDnDiag, _
120, 340, 80, 60)
e.Graphics.FillRectangle(brushLtHorz, _
220, 340, 80, 60)
e.Graphics.FillRectangle(brushUpDiag, _
320, 340, 80, 60)
e.Graphics.FillRectangle(brushLtVert, _
420, 340, 80, 60)
End Sub
End Class
Function Main() As Integer
Dim frmStart As Starter = New Starter
Application.Run(frmStart)
Return 0
End Function
End Module
This would produce:
If you use the above constructor to fill out a shape, the selected pattern would be drawn on top of a black color used as the background. If you want to use a different background, use the following constructor to initialize the brush: Public Sub New(hatchstyle As HatchStyle, foreColor As Color, backColor As Color) The backColor argument passed as a Color value will be used as the background Color. Here are examples of specifying the back color: Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Module Exercise
Public Class Starter
Inherits Form
Dim components As System.ComponentModel.Container
Public Sub New()
InitializeComponent()
End Sub
Public Sub InitializeComponent()
End Sub
Private Sub FormPaint(ByVal sender As Object, _
ByVal e As PaintEventArgs) _
Handles MyBase.Paint
Dim hsBrush As HatchStyle() = _
{ _
HatchStyle.BackwardDiagonal, _
HatchStyle.Cross, _
HatchStyle.Divot, _
HatchStyle.DarkDownwardDiagonal, _
HatchStyle.DarkHorizontal, _
HatchStyle.ForwardDiagonal, _
HatchStyle.DarkUpwardDiagonal, _
HatchStyle.DarkVertical, _
HatchStyle.HorizontalBrick, _
HatchStyle.DashedDownwardDiagonal, _
HatchStyle.DashedHorizontal, _
HatchStyle.DashedVertical, _
HatchStyle.LargeCheckerBoard, _
HatchStyle.DiagonalBrick, _
HatchStyle.Horizontal, _
HatchStyle.DiagonalCross, _
HatchStyle.DottedGrid, _
HatchStyle.DottedDiamond, _
HatchStyle.LightUpwardDiagonal, _
HatchStyle.LargeConfetti, _
HatchStyle.LargeGrid, _
HatchStyle.LightDownwardDiagonal, _
HatchStyle.OutlinedDiamond, _
HatchStyle.LightHorizontal, _
HatchStyle.LightVertical _
}
Dim ForeColors As Color() = _
{ _
Color.FromArgb(0, 0, 255), Color.FromArgb(200, 0, 0), _
Color.Salmon, Color.Navy, Color.Pink, _
Color.FromArgb(255, 0, 255), Color.FromArgb(255, 128, 0), _
Color.FromArgb(0, 128, 192), Color.Green, _
Color.Firebrick, Color.Fuchsia, Color.Moccasin, _
Color.Goldenrod, Color.Gainsboro, Color.Khaki, _
Color.Maroon, Color.DarkCyan, Color.Purple, _
Color.MistyRose, Color.RoyalBlue, Color.Red, _
Color.SaddleBrown, Color.Olive, Color.SeaGreen, _
Color.PowderBlue _
}
Dim BackColors As Color() = _
{ _
Color.Azure, Color.DarkBlue, Color.AntiqueWhite, _
Color.Aqua, Color.DarkGray, Color.Aquamarine, _
Color.Azure, Color.Beige, Color.DarkGoldenrod, _
Color.Bisque, Color.DarkKhaki, Color.BlanchedAlmond, _
Color.Brown, Color.DarkCyan, Color.AliceBlue, _
Color.BurlyWood, Color.CadetBlue, Color.DarkMagenta, _
Color.Coral, Color.Chartreuse, Color.CornflowerBlue, _
Color.Cornsilk, Color.Crimson, Color.Cyan, _
Color.DarkGreen _
}
Dim rnd As Random = New Random()
Dim brushBackDiag As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushCross As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushDarkDown As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushDarkHorz As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushDarkUpDiag As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushVertical As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushDashDnDiag As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushDashHorz As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushDashUpDiag As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushDashVert As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushDiagBrisk As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushDiagCross As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushDivot As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushDotDiamond As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushDottedGrid As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushForDiag As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushHorz As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushHorzBrick As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushLgChkBoard As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushLgConfetti As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushLgGrid As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushLtDnDiag As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushLtHorz As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushUpDiag As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
Dim brushLtVert As HatchBrush = _
New HatchBrush(hsBrush(rnd.Next(25)), _
ForeColors(rnd.Next(25)), BackColors(rnd.Next(25)))
e.Graphics.FillRectangle(brushBackDiag, 20, 20, 80, 60)
e.Graphics.FillRectangle(brushCross, 120, 20, 80, 60)
e.Graphics.FillRectangle(brushDarkDown, 220, 20, 80, 60)
e.Graphics.FillRectangle(brushDarkHorz, 320, 20, 80, 60)
e.Graphics.FillRectangle(brushDarkUpDiag, 420, 20, 80, 60)
e.Graphics.FillRectangle(brushVertical, 20, 100, 80, 60)
e.Graphics.FillRectangle(brushDashDnDiag, 120, 100, 80, 60)
e.Graphics.FillRectangle(brushDashHorz, 220, 100, 80, 60)
e.Graphics.FillRectangle(brushDashUpDiag, 320, 100, 80, 60)
e.Graphics.FillRectangle(brushDashVert, 420, 100, 80, 60)
e.Graphics.FillRectangle(brushDashVert, 20, 180, 80, 60)
e.Graphics.FillRectangle(brushDiagBrisk, 120, 180, 80, 60)
e.Graphics.FillRectangle(brushDiagCross, 220, 180, 80, 60)
e.Graphics.FillRectangle(brushDivot, 320, 180, 80, 60)
e.Graphics.FillRectangle(brushDotDiamond, 420, 180, 80, 60)
e.Graphics.FillRectangle(brushDottedGrid, 20, 260, 80, 60)
e.Graphics.FillRectangle(brushForDiag, 120, 260, 80, 60)
e.Graphics.FillRectangle(brushHorz, 220, 260, 80, 60)
e.Graphics.FillRectangle(brushHorzBrick, 320, 260, 80, 60)
e.Graphics.FillRectangle(brushLgChkBoard, 420, 260, 80, 60)
e.Graphics.FillRectangle(brushLgGrid, 20, 340, 80, 60)
e.Graphics.FillRectangle(brushLtDnDiag, 120, 340, 80, 60)
e.Graphics.FillRectangle(brushLtHorz, 220, 340, 80, 60)
e.Graphics.FillRectangle(brushUpDiag, 320, 340, 80, 60)
e.Graphics.FillRectangle(brushLtVert, 420, 340, 80, 60)
End Sub
End Class
Function Main() As Integer
Dim frmStart As Starter = New Starter
Application.Run(frmStart)
Return 0
End Function
End Module
This would produce:
At any time, to find out the color used to paint a pattern, you can access the brush's ForegroundColor property. To know the color used as background, you can access the brush's BackgroundColor property. To know the hatch style used on the current brush, you can access its HatchStyle property. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||
| Previous | Copyright © 2008-2009, yevol.com | Next |
|
|
||