Visual Basic NET BASICS Lesson 15 Lines and
Visual Basic. NET BASICS Lesson 15 Lines and Shapes
Objectives n n Explain the coordinate system used to position graphical objects on a form. Draw lines from code. Draw boxes and rectangles from code. Draw circles and ellipses from code. 2
Objectives (continued) n n Draw polygrams. Create images made up of multiple objects drawn on a form. 3
Drawing Objects Using Code n n Visual Basic. NET provides a single Graphic object with a large number of methods that allow a program to draw on a form. A Graphics object is a general-purpose drawing surface that may be used with a number of graphical tools in Visual Studio. NET. 4
Understanding Coordinates n n In order to begin working with drawing objects, you must first understand the coordinate system on a form. The top-left point on a form has an X coordinate of zero and a Y coordinate of zero. ¡ n To use the Draw. Line method, you need to think in terms of X and Y rather than top and left. The combination of the X and Y value is called a Point. 5
Understanding Pens and Brushes n After specifying the location of an object, you will also have to specify a Pen or Brush to use to draw the object. ¡ ¡ A pen is used to draw lines or the outline of a shape. A brush is used to fill in shapes or to draw text. 6
Using the Draw. Line Method n n To use the Draw. Line method, you must specify the endpoints of the line you want to draw and the pen you want to use to draw the line. You can also create a Pen inside the Draw. Line statement. 7
Drawing Rectangles and Boxes n Rectangles and boxes can be drawn in two different ways: ¡ ¡ n The Draw. Rectangle method uses a Pen object and is used to draw the outline of a rectangle. The Fill. Rectangle method uses a Brush object and is used to draw a rectangle that is filled in. The rectangle is created with an X and Y location for its top-left corner and two other parameters that will specify the width and height properties. 8
Drawing Circles and Ellipses n n n Like rectangles, circles and ellipses can either be drawn as outlines or as filled objects. The parameters for the Draw. Ellipse method are a Pen object, the location of the ellipse, and the width and height of the ellipse. The Fill. Ellipse method uses the same location and size parameters, but uses a Brush object in place of a Pen object. 9
Drawing Polygons n n A polygon is any shape with three or more sides. The Draw. Polygon method takes a Pen as its first parameter. The second parameter is an array of points. The array has one element less than the number of sides in the polygon. 10
Locating the Mouse n The Mouse. Down event returns a number of useful pieces of information. ¡ ¡ n It returns the location on the form where the mouse was pointing when the mouse button was pressed. This variable, ” has an X and Y coordinate and holds the location where the mouse was clicked. This will allow your program to draw wherever the user wishes to draw. 11
Clearing the Form n The form can be cleared in two ways: ¡ ¡ Parts of the form may be cleared by drawing new shapes over existing shapes. Another way is to use the Clear method of the Graphics objects. 12
Summary n n n You can use the Draw. Line method to draw a line on a form using Visual Basic. NET code. There are two ways to draw most shapes on a form. A Draw method creates an outline drawing of the object. The Fill method creates an object that is filled with a color specified by a brush color. A Pen object is created with a color and may also have a line specified for it. 13
Summary (continued) n n Rectangles (including squares) are created using the Draw. Rectangle and Fill. Rectangle methods. Rectangles are objects that can be declared with a starting point and width and height. Once they are declared, rectangle objects can be used to draw both rectangles and ellipses. Ellipses (including circles) are created using the Draw. Ellipse and Fill. Ellipse methods. 14
Summary (continued) n n n Once you have drawn a line, rectangle, ellipse, or other shape with a graphic method, you cannot manipulate it. The graphic methods draw directly on a form. The Mouse. Down event returns information about the location of the mouse and the button that was used to click on the mouse. The Clear method of the Graphics object is used to erase the whole form. 15
Summary (continued) n Distance and position on a form is measured using coordinates. The top-left corner of a form has the coordinates (0, 0). All coordinates are positive. As you move toward the bottom of a form, the X-coordinate value increases. As you move toward the right of a form, the Y-coordinate value increases. 16
- Slides: 16