Drawing Shapes Using the Brush Class


The next example uses the Brush class to draw shapes such as rectangles, ellipses, pie charts, and polygons. the Brush class is an abstract base class. To instantiate a Brush object, you use classes derived from Brush such as SolidBrush, TextureBrush, and LinearGradientBrush.

The Brush and SolidBrush classes are in the System.Drawing namespace. However, the TextureBrush and LinearGradientBrush are in the System.Drawing.Drawing2D namespace. This is what each brush class achieves:

  • SolidBrush fills a shape with a solid color.

  • TextureBrush fills a shape with a bitmap. When constructing this brush, you also specify a bounding rectangle and a wrap mode. The bounding rectangle specifies what portion of the bitmap to use for the brush — you don't need to use the entire bitmap if you don't want to. The wrap mode has a number of options, including Tile, which tiles the texture, TileFlipX, TileFlipY, and TileFlipXY, which tile while flipping the image for successive tiles. You can create very interesting and imaginative effects using the TextureBrush.

  • LinearGradientBrush encapsulates a brush that draws a gradient of two colors, where the first color transitions to the second color at a specified angle. You specify angles in terms of degrees. An angle of zero specifies that the colors will transition from left to right. An angle of 90 degrees means that the colors will transition from top to bottom.

One more brush that I will mention is the PathGradientBrush, which creates an elaborate shading effect, where the shading runs from the center of the path to the edge of the path. This brush reminds me of when I was a child, and I would shade maps with colored pencils, making the color darker at the boundary between different states or countries.

Important

Always call Dispose( ) on Brush objects.

Just as for Graphics objects and Pen objects, it is important to call Dispose() on Brush objects that you create, or use the using block; otherwise, your application may deplete the Windows resources. In the following Try It Out, you will fill some shapes using some brushes.

Try It Out – Brush Example

image from book

Follow these steps to see brushes in action:

  1. Create a new Windows application called UsingBrushes in the directory C:\BegVCSharp\ Chapter30.

  2. Add a using directive for System.Drawing.Drawing2D for the LinearGradientBrush to the top of the code:

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; 
  3. In the constructor of the Form1 class, add a call to SetStyle() after the call to InitializeComponent():

    public Form1() {    InitializeComponent(); SetStyle(ControlStyles.Opaque, true); }
  4. Now, add an OnPaint() method to your class:

     protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, ClientRectangle); g.FillRectangle(Brushes.Red, new Rectangle(10, 10, 50, 50)); Brush linearGradientBrush = new LinearGradientBrush( new Rectangle(10, 60, 50, 50), Color.Blue, Color.White, 45); g.FillRectangle(linearGradientBrush, new Rectangle(10, 60, 50, 50)); // Manually call Dispose(). linearGradientBrush.Dispose(); g.FillEllipse(Brushes.Aquamarine, new Rectangle(60, 20, 50, 30)); g.FillPie(Brushes.Chartreuse, new Rectangle(60, 60, 50, 50), 90, 210); g.FillPolygon(Brushes.BlueViolet, new Point[] { new Point(110, 10), new Point(150, 10), new Point(160, 40), new Point(120, 20), new Point(120, 60), }); } 

  5. When you compile and run this program, it will display as shown in Figure 30-10.

    image from book
    Figure 30-10

How It Works

The first thing to remark about is the call to SetStyle() in the form's constructor. SetStyle() is a method of the Form class:

SetStyle(ControlStyles.Opaque, true);

This method changes the behavior of the Form class so that it will not automatically draw the background of the window. If you include this line, but don't draw the background of the window yourself, then anything underneath the window at the time of creation shows through, which is not what you want. Thus your next activity is to draw your own background onto the client area of the window.

Just as with the Pens class, there is a Brushes class that contains properties for getting approximately 150 brushes, one for each predefined color. You use this class to get most of the brushes in this example, with the exception of the LinearGradientBrush, which you create yourself.

The first call to FillRectangle() draws the background of the client area of your window:

g.FillRectangle(Brushes.White, ClientRectangle);

The creation of the LinearGradientBrush takes a rectangle, specifying its size, the two colors to be used for the gradient, and the angle (in this case 45):

Brush linearGradientBrush = new LinearGradientBrush(         new Rectangle(10, 60, 50, 50), Color.Blue, Color.White, 45); g.FillRectangle(linearGradientBrush, new Rectangle(10, 60, 50, 50)); linearGradientBrush.Dispose();

When you specified the rectangle for the brush, you used a rectangle of width 50 and height 50, which is the same size as the rectangle used when you defined the brush. The result is that the brush area just fits the rectangle. Try changing the rectangle defined in the creation of the brush so that the width and height are 10 and see what happens. Also, try changing the angle to different values to see the change in effect.

image from book




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net