Drawing Graphics Objects

Previous sections of this chapter described brushes and pens and how to use them to draw lines or perform other basic drawing operations. In addition to these basic capabilities, the Graphics object exposes methods that help draw various types of shapes. These include rectangles, ellipses, polygons, arcs, and pies.

Each of the drawing shapes discussed in this section has overloads that make it possible to create the shape in different ways, but produce the same result. For each shape drawing method, there are also corresponding fill methods that paint the inside of the shape with a brush. For example, for the DrawRectangle method, there is also a FillRectangle method.

Rectangles

In addition to drawing lines, one of the simplest shapes to draw is the rectangle. Listing 9.10 shows how to draw a rectangle.

Listing 9.10 Drawing a Rectangle
 protected override void OnPaint(PaintEventArgs pea) {    base.OnPaint(pea);    const int borderOffset = 15;    int x = borderOffset;    int y = borderOffset;    int width  = ClientRectangle.Width  - (borderOffset * 2);    int height = ClientRectangle.Height - (borderOffset * 2);    Pen       pen  = new Pen(ForeColor);    Rectangle rect = new Rectangle(x, y, width, height);    pea.Graphics.DrawRectangle(pen, rect); } 

The call to DrawRectangle in Listing 9.10 simply specifies the pen to draw with and the bounds of the rectangle. Figure 9.8 shows this program executing. Setting ResizeRedraw to true in the constructor enables the rectangle to redraw to a specified offset from the border each time the window is resized.

Figure 9.8. Rectangle drawing demo.

graphics/09fig08.gif

Ellipses

Drawing circles and ellipses consists of defining the pen they will be drawn with and the rectangle they are bounded by. Listing 9.11 shows how to draw an ellipse.

Listing 9.11 Drawing an Ellipse
 protected override void OnPaint(PaintEventArgs pea) {    base.OnPaint(pea);    const int borderOffset = 15;    int x = borderOffset;    int y = borderOffset;    int width  = ClientRectangle.Width  - (borderOffset * 2);    int height = ClientRectangle.Height - (borderOffset * 2);    Pen       pen  = new Pen(ForeColor);    Rectangle rect = new Rectangle(x, y, width, height);    pea.Graphics.DrawEllipse(pen, rect); } 

The ellipse drawn in Listing 9.11 is done the same way as in the rectangle drawing demonstration in Listing 9.10. Figure 9.9 shows how the program in Listing 9.11 appears when run.

Figure 9.9. Ellipse drawing demo.

graphics/09fig09.gif

Polygons

A polygon is a set of points that defines a shape. The points are defined by an array of Point structures. When you're drawing the polygon, lines are drawn between adjacent points in the array, beginning with the first point. The last line will be drawn between the last point and the first point in the array to close the polygon. Listing 9.12 shows how to draw a polygon.

Listing 9.12 Drawing a Polygon
 protected override void OnPaint(PaintEventArgs pea) {    base.OnPaint(pea);    const int borderOffset = 15;    int width  = ClientRectangle.Width;    int height = ClientRectangle.Height;    Point[] points = new Point[3];    points[0] = new Point(width/2, borderOffset);    points[1] = new Point(width - borderOffset, height - borderOffset);    points[2] = new Point(borderOffset, height - borderOffset);    Pen pen = new Pen(ForeColor);    pea.Graphics.DrawPolygon(pen, points); } 

In Listing 9.12, the array of points is defined with three elements. Each point is set to represent a point in a triangle. Recall that there are only three because the last line will be drawn from the last point to the first. Figure 9.10 shows what the polygon looks like when drawn.

Figure 9.10. Polygon drawing demo.

graphics/09fig10.gif

Arcs

An arc is part of an ellipse that is drawn, beginning at one point and continuing for a specified number of degrees. Specified at 360 degrees, an arc would look just like an ellipse. Arcs are defined within the bounds of a rectangle. Listing 9.13 shows how to draw an arc.

Listing 9.13 Drawing an Arc
 protected override void OnPaint(PaintEventArgs pea) {    base.OnPaint(pea);    const int borderOffset = 15;    int x = borderOffset;    int y = borderOffset;    int width  = ClientRectangle.Width  - (borderOffset * 2);    int height = ClientRectangle.Height - (borderOffset * 2);    Pen       pen  = new Pen(ForeColor);    Rectangle rect = new Rectangle(x, y, width, height);    int startAngle = 45;    int degrees    = 270;    pea.Graphics.DrawArc(pen, rect, startAngle, degrees); } 

Listing 9.13 shows how an arc is defined within the bounds of a rectangle. Calling it requires a beginning angle and the amount of degrees to draw. Figure 9.11 shows what this arc looks like when drawn.

Figure 9.11. Arc drawing demo.

graphics/09fig11.gif

Pies

A pie is similar to an arc, except it connects the beginning and end of the arc with lines that connect to the center of its enclosing rectangle. Listing 9.14 shows how to draw a pie.

Listing 9.14 Drawing a Pie
 protected override void OnPaint(PaintEventArgs pea) {    base.OnPaint(pea);    const int borderOffset = 15;    int x = borderOffset;    int y = borderOffset;    int width  = ClientRectangle.Width  - (borderOffset * 2);    int height = ClientRectangle.Height - (borderOffset * 2);    Pen       pen  = new Pen(ForeColor);    Rectangle rect = new Rectangle(x, y, width, height);    int startAngle = 45;    int degrees    = 270;    pea.Graphics.DrawPie(pen, rect, startAngle, degrees); } 

Listing 9.14 demonstrates that drawing an arc and a pie are pretty much the same except the method call is DrawPie. Figure 9.12 shows what this pie program looks like when executed.

Figure 9.12. Pie drawing demo.

graphics/09fig12.gif

SHOP TALK:
THE RIGHT TOOL FOR THE RIGHT THE RIGHT TOOL FOR THE RIGHT JOB

GDI+ is a lot of fun and very powerful. You may even be tempted to take it to the limit with drawing 3D objects and animation. The truth is that GDI+ is really good for 2D drawing, but there are better tools for more sophisticated graphics. If you are interested in 3D graphics, a good alternative would be DirectX. Microsoft has updated this so that version 9 and later have a managed interface, accessible via C#. It is called Managed DirectX and performs excellently for 3D animation and gaming. You can find the DirectX SDK on Microsoft's Web site at http://www.Microsoft.com.



C# Builder KickStart
C# Builder KickStart
ISBN: 672325896
EAN: N/A
Year: 2003
Pages: 165

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