Drawing Lines, Rectangles and Ovals
This section presents Graphics methods for drawing lines, rectangles and ovals. Each of the drawing methods has several overloaded versions. Methods that draw hollow shapes typically require as arguments a Pen and four ints. Methods that draw solid shapes typically require as arguments a Brush and four ints. The first two int arguments represent the coordinates of the upper-left corner of the shape (or its enclosing area), and the last two ints indicate the shape's (or enclosing area's) width and height. Figure 17.13 summarizes several Graphics methods and their parameters. [Note: Many of these methods are overloadedconsult the documentation for a complete listing (msdn2.microsoft.com/en-us/library/system.drawing.graphics).]
Graphics Drawing Methods and Descriptions |
---|
DrawLine( Pen p, int x1, int y1, int x2, int y2 ) |
Draws a line from (x1, y1) to (x2, y2). The Pen determines the line's color, style and width. |
DrawRectangle( Pen p, int x, int y, int width, int height ) |
Draws a rectangle of the specified width and height. The top-left corner of the rectangle is at point (x, y). The Pen determines the rectangle's color, style and border width. |
FillRectangle( Brush b, int x, int y, int width, int height ) |
Draws a solid rectangle of the specified width and height. The top-left corner of the rectangle is at point (x, y). The Brush determines the fill pattern inside the rectangle. |
DrawEllipse(Pen p, int x, int y, int width, int height ) |
Draws an ellipse inside a bounding rectangle of the specified width and height. The top-left corner of the bounding rectangle is located at (x, y). The Pen determines the color, style and border width of the ellipse. |
FillEllipse( Brush b, int x, int y, int width, int height ) |
Draws a filled ellipse inside a bounding rectangle of the specified width and height. The topleft corner of the bounding rectangle is located at (x, y). The Brush determines the pattern inside the ellipse. |
The application in Fig. 17.14 draws lines, rectangles and ellipses. In this application, we also demonstrate methods that draw filled and unfilled shapes.
Figure 17.14. Demonstration of methods that draw lines, rectangles and ellipses.
1 // Fig. 17.14: LinesRectanglesOvals.cs 2 // Demonstrating lines, rectangles and ovals. 3 using System; 4 using System.Drawing; 5 using System.Windows.Forms; 6 7 // draw shapes on Form 8 public partial class LinesRectanglesOvals : Form 9 { 10 // default constructor 11 public LinesRectanglesOvals() 12 { 13 InitializeComponent(); 14 } // end constructor 15 16 // override Form OnPaint method 17 protected override void OnPaint( PaintEventArgs paintEvent ) 18 { 19 // get graphics object 20 Graphics g = paintEvent.Graphics; 21 SolidBrush brush = new SolidBrush( Color.Blue ); 22 Pen pen = new Pen( Color.AliceBlue ); 23 24 // create filled rectangle 25 g.FillRectangle( brush, 90, 30, 150, 90 ); 26 27 // draw lines to connect rectangles 28 g.DrawLine( pen, 90, 30, 110, 40 ); 29 g.DrawLine( pen, 90, 120, 110, 130 ); 30 g.DrawLine( pen, 240, 30, 260, 40 ); 31 g.DrawLine( pen, 240, 120, 260, 130 ); 32 33 // draw top rectangle 34 g.DrawRectangle( pen, 110, 40, 150, 90 ); 35 36 // set brush to red 37 brush.Color = Color.Red; 38 39 // draw base Ellipse 40 g.FillEllipse( brush, 280, 75, 100, 50 ); 41 42 // draw connecting lines 43 g.DrawLine( pen, 380, 55, 380, 100 ); 44 g.DrawLine( pen, 280, 55, 280, 100 ); 45 46 // draw Ellipse outline 47 g.DrawEllipse( pen, 280, 30, 100, 50 ); 48 } // end method OnPaint 49 } // end class LinesRectanglesOvals |
Methods FillRectangle and DrawRectangle (lines 25 and 34) draw rectangles on the screen. For each method, the first argument specifies the drawing object to use. The FillRectangle method uses a Brush object (in this case, an instance of SolidBrusha class that derives from Brush), whereas the DrawRectangle method uses a Pen object. The next two arguments specify the coordinates of the upper-left corner of the bounding rectangle, which represents the area in which the rectangle will be drawn. The fourth and fifth arguments specify the rectangle's width and height. Method DrawLine (lines 2831) takes a Pen and two pairs of ints, specifying the start and end of a line. The method then draws a line, using the Pen object.
Methods FillEllipse and DrawEllipse (lines 40 and 47) each provide overloaded versions that take five arguments. In both methods, the first argument specifies the drawing object to use. The next two arguments specify the upper-left coordinates of the bounding rectangle representing the area in which the ellipse will be drawn. The last two arguments specify the bounding rectangle's width and height, respectively. Figure 17.15 depicts an ellipse bounded by a rectangle. The ellipse touches the midpoint of each of the four sides of the bounding rectangle. The bounding rectangle is not displayed on the screen.
Figure 17.15. Ellipse bounded by a rectangle.
Drawing Arcs
|