Game Development of .Nettrix: GDI+ and Collision Detection - Creating Gradients
(Page 3 of 22 )
In the previous section, you saw some code samples used to create solid red rectangles via a SolidBrush object. GDI+ allows the programmer to go beyond flat colors and create linear and path gradients, using special gradient brushes that provide very interesting effects.
GDI+ has features to create horizontal, vertical, and diagonal linear gradients. You can create linear gradients in which the colors change uniformly (the default behavior), or in a nonuniform way by using the Blend property of the gradient brush.
The sample code here shows how to create a uniform gradient brush and draw a rectangle with color changing from red to blue from the upper-left to the lower-right vertex:
Graphics graph; Drawing2D.LinearGradientBrush linGrBrush;
graph = Graphics.FromHwnd(picSource.Handle);
linGrBrush = new Drawing2D.LinearGradientBrush(
new Point(10, 20), // Start gradient point.
new Point(23, 27), // End gradient point.
Color.FromArgb(255, 255, 0, 0), // Red
Color.FromArgb(255, 0, 0, 255)) // Blue
graph.FillRectangle(linGrBrush, 10, 20, 13, 7);
Note: The most important part of this sample code is the color definition using the FromArgb method of the Color object. As you can see, each color in GDI+ is always defined by four values: the red, green, blue (RGB) values used by the classic GDI functions, plus the alpha (A) value, which defines the transparency of the color. In the preceding example, you use an alpha value of 255 for both col ors, so they will be totally opaque. Using a value of 128, you create a 50 percent transparent color, so any graphics below are shown through the rectangle. Setting alpha to zero means that the color will be 100 percent transparent, or totally invisible. The in-between values allow different degrees of transparency. |
Path gradients allow you to fill a shape using a color pattern defined by a specified path. The path can be composed of points, ellipses, and rectangles, and you can specify one color for the center of the path and a different color for each of the points in the path, allowing the creation of many different effects.
To draw an image using gradient paths, you must create a PathGradientBrush object, based on a GraphicsPath object that is defined by a sequence of lines, curves, and shapes. The code here shows how to draw the same rectangle from the previous examples, using a gradient that starts with a green color in the center of the rectangle and finishes with a blue color at the edges:
Graphics graph;
Rectangle rectSquare;
Drawing2D.GraphicsPath graphPath;
Drawing2D.PathGradientBrush brushSquare;
graph = Graphics.FromHwnd(picSource.Handle);
// Create a path consisting of one rectangle.
graphPath = new Drawing2D.GraphicsPath();
rectSquare = new Rectangle(10, 20, 23, 27);
graphPath.AddRectangle(rectSquare);
brushSquare = new Drawing2D.PathGradientBrush(graphPath);
brushSquare.CenterColor = Color.FromArgb(255, 0, 255, 0);
brushSquare.SurroundColors = new Color(){Color.FromArgb(255, 0, 0, 255)};
// Create the rectangle from the path.
Graph.FillPath(brushSquare, graphPath);
Note: We won’t go into much detail here about brushes and paths. Refer to the .NET SDK documentation for some extra examples about how to use these features. For a complete overview about this topic, look for “System.Drawing.Drawing2D Hierarchy” in the online help. |
In the next section we’ll discuss collision detection, after which you’ll have an understanding of all the basic concepts you need to implement your first game.
This chapter is from Beginning .NET Game Programming in C#, by David Weller, et al., (Apress, 2004, ISBN: 1590593197). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Collision Detection >>
More .NET Articles
More By Apress Publishing