This document discusses how to draw various shapes and lines on a form in Visual Basic.NET using code. It explains that a Graphics object provides methods for drawing, and describes how to use the DrawLine, DrawRectangle, FillRectangle, DrawEllipse, and FillEllipse methods to draw lines, rectangles, and ellipses by specifying locations and sizes. It also covers drawing polygons and clearing the form, and notes that the coordinate system increases positively from top-left.
2. 2
Objectives
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.
4. 4
Drawing Objects Using Code
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.
5. 5
Understanding Coordinates
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.
To use the DrawLine 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.
6. 6
Understanding Pens and
Brushes
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.
7. 7
Using the DrawLine Method
To use the DrawLine 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
DrawLine statement.
8. 8
Drawing Rectangles and
Boxes
Rectangles and boxes can be drawn in two
different ways:
The DrawRectangle method uses a Pen object
and is used to draw the outline of a rectangle.
The FillRectangle 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.
9. 9
Drawing Circles and Ellipses
Like rectangles, circles and ellipses can
either be drawn as outlines or as filled
objects.
The parameters for the DrawEllipse method
are a Pen object, the location of the ellipse,
and the width and height of the ellipse.
The FillEllipse method uses the same
location and size parameters, but uses a
Brush object in place of a Pen object.
10. 10
Drawing Polygons
A polygon is any shape with three or
more sides.
The DrawPolygon 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.
11. 11
Locating the Mouse
The MouseDown event returns a number of
useful pieces of information.
It returns the location on the form where the
mouse was pointing when the mouse button was
pressed.
This variable, ”e,” 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.
12. 12
Clearing the Form
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.
13. 13
Summary
You can use the DrawLine 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.
14. 14
Summary (continued)
Rectangles (including squares) are created
using the DrawRectangle and FillRectangle
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 DrawEllipse and FillEllipse methods.
15. 15
Summary (continued)
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 MouseDown 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.
16. 16
Summary (continued)
Distance and position on a form is mea-
sured 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-coordi-
nate value increases. As you move toward
the right of a form, the Y-coordinate value
increases.