Section 17.7. Drawing Arcs


17.7. Drawing Arcs

Arcs are portions of ellipses and are measured in degrees, beginning at a starting angle and continuing for a specified number of degrees called the arc angle. An arc is said to sweep (traverse) its arc angle, beginning from its starting angle. Arcs that sweep in a clockwise direction are measured in positive degrees, whereas arcs that sweep in a counterclockwise direction are measured in negative degrees. Figure 17.16 depicts two arcs. Note that the arc at the left of the figure sweeps upward from zero degrees to approximately 110 degrees. Similarly, the arc at the right of the figure sweeps downward from zero degrees to approximately 110 degrees.

Figure 17.16. Positive and negative arc angles.


Note the dashed boxes around the arcs in Fig. 17.16. Each arc is drawn as part of an oval (the rest of which is not visible). When drawing an oval, we specify the oval's dimensions in the form of a bounding rectangle that encloses the oval. The boxes in Fig. 17.16 correspond to these bounding rectangles. The Graphics methods used to draw arcsDrawArc, DrawPie and FillPieare summarized in Fig. 17.17.

Figure 17.17. Graphics methods for drawing arcs.

Graphics methods and descriptions

Note: Many of these methods are overloadedconsult the documentation for a complete listing.

 DrawArc(ByVal p As Pen, ByVal x As Integer, ByVal y As Integer, _    ByVal width As Integer, ByVal height As Integer, _    ByVal startAngle As Integer, ByVal sweepAngle As Integer) 


Draws an arc beginning from angle startAngle (in degrees) and sweeping sweepAngle degrees. The ellipse is defined by a bounding rectangle of width, height and upper-left corner (x,y). The Pen determines the color, border width and style of the arc.

 DrawPie(ByVal p As Pen, ByVal x As Integer, ByVal y As Integer, _    ByVal width As Integer, ByVal height As Integer, _    ByVal startAngle As Integer, ByVal sweepAngle As Integer) 


Draws a pie section of an ellipse beginning from angle startAngle (in degrees) and sweeping sweepAngle degrees. The ellipse is defined by a bounding rectangle of width, height and upper-left corner (x,y). The Pen determines the color, border width and style of the arc.

 FillPie(ByVal b As Brush, ByVal x As Integer, ByVal y As Integer, _    ByVal width As Integer, ByVal height As Integer, _    ByVal startAngle As Integer, ByVal sweepAngle As Integer) 


Functions similarly to DrawPie, except draws a solid arc (i.e., a sector). The Brush determines the fill pattern for the solid arc.

The program in Fig. 17.18 draws six images (three arcs and three filled pie slices) to demonstrate the arc methods listed in Fig. 17.17. To illustrate the bounding rectangles that determine the sizes and locations of the arcs, the arcs are displayed inside red rectangles that have the same x-y coordinates, width and height arguments as those that define the bounding rectangles for the arcs.

Figure 17.18. Drawing various arcs on a Form.

  1  ' Fig. 17.18: FrmDrawingArcs.vb  2  ' Drawing various arcs on a Form.  3  Public Class FrmDrawArcs  4     ' draw arcs  5     Private Sub FrmDrawArcs_Paint(ByVal sender As Object, _  6        ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint  7        ' get graphics object  8        Dim graphicsObject As Graphics = e.Graphics  9        Dim rectangle1 As New Rectangle(15, 35, 80, 80) 10        Dim brush1 As New SolidBrush(Color.Firebrick) 11        Dim pen1 As New Pen(brush1, 1) 12        Dim brush2 As New SolidBrush(Color.DarkBlue) 13        Dim pen2 As New Pen(brush2, 1) 14 15        ' start at 0 and sweep 360 degrees 16        graphicsObject.DrawRectangle(pen1, rectangle1) 17        graphicsObject.DrawArc(pen2, rectangle1, 0, 360) 18 19        ' start at 0 and sweep 110 degrees 20        rectangle1.Location = New Point(100, 35) 21        graphicsObject.DrawRectangle(pen1, rectangle1) 22        graphicsObject.DrawArc(pen2, rectangle1, 0, 110) 23 24        ' start at 0 and sweep -270 degrees 25        rectangle1.Location = New Point(185, 35) 26        graphicsObject.DrawRectangle(pen1, rectangle1) 27        graphicsObject.DrawArc(pen2, rectangle1, 0, -270) 28 29        ' start at 0 and sweep 360 degrees 30        rectangle1.Location = New Point(15, 120) 31        rectangle1.Size = New Size(80, 40) 32        graphicsObject.DrawRectangle(pen1, rectangle1) 33        graphicsObject.FillPie(brush2, rectangle1, 0, 360) 34 35        ' start at 270 and sweep -90 degrees 36        rectangle1.Location = New Point(100, 120) 37        graphicsObject.DrawRectangle(pen1, rectangle1) 38        graphicsObject.FillPie(brush2, rectangle1, 270, -90) 39 40        ' start at 0 and sweep -270 degrees 41        rectangle1.Location = New Point(185, 120) 42        graphicsObject.DrawRectangle(pen1, rectangle1) 43        graphicsObject.FillPie(brush2, rectangle1, 0, -270) 44     End Sub ' FrmDrawArcs_Paint 45  End Class ' FrmDrawArcs 

Lines 913 create the objects that we need to draw arcsa Graphics object, a Rectangle, SolidBrushes and Pens. Lines 1617 then draw a rectangle and an arc inside the rectangle. The arc sweeps 360 degrees, forming a circle. Line 20 changes the location of the Rectangle by setting its Location property to a new Point. The Point constructor takes as arguments the x- and y-coordinates of thenew point. The Location property determines the upper-left corner of the Rectangle. After drawing the rectangle, the program draws an arc that starts at 0 degrees and sweeps 110 degrees. Because the angles increase in a clockwise direction, the arc sweeps downward. Lines 2527 perform similar functions, except that the specified arc sweeps 270 degrees. The Size property of a Rectangle determines the arc's height and width.

Line 31 sets the Size property to a new Size object, which changes the size of the rectangle. The remainder of the program is similar to the portions described above, except that a SolidBrush is used with method FillPie. The resulting arcs, which are filled, can be seen in the bottom half of the sample output (Fig. 17.18).



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

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