Drawing Lines and Shapes

See Also

Although clearing the drawing area and plotting individual points can be useful, the most interesting graphics methods draw complete lines and shapes.

Drawing Lines

To draw a line between two coordinates, use the simple form of the Line method, which has this syntax:

[object.]Line [(x1, y1)]–(x2, y2)[, color]

Object is optional; if omitted, the method draws on the form to which the code is attached (the current form). The first pair of coordinates is also optional. As with all coordinate values, the x and y arguments can be either integer or fractional numbers. For example, this statement draws a slanted line on a form.

Line (500, 500)–(2000, 2000)

Visual Basic draws a line that includes the first end point, but not the last end point. This behavior is useful when drawing a closed figure from point to point. To draw the last point, use this syntax:

PSet [Step] (0, 0)[, color]

The first pair of coordinates (x1, y1) is optional. If you omit these coordinates, Visual Basic uses the object’s current x, y location (drawing coordinates) as the end point. The current location can be specified with the CurrentX and CurrentY properties, but otherwise it is equal to the last point drawn by a previous graphics or Print method. If you haven’t previously used a graphics or Print method or set CurrentX and CurrentY, the default location is the object’s upper-left corner.

For example, the following statements draw a triangle by connecting three points.

' Set x-coordinate of starting point.
CurrentX = 1500
' Set y-coordinate of starting point.
CurrentY = 500
' Draw line down and right of starting point.
Line -(3000, 2000)
' Draw line to the left of current point.
Line -(1500, 2000)   
' Draw line up and right to starting point.
Line -(1500, 500)

The results are shown in Figure 12.14.

Figure 12.14   A triangle drawn with the Line method

The Blanker application uses the Line method to create interesting patterns. To view this, from the Options menu, choose Crossfire, and then choose the Start Demo button.

The Step Keyword

The PSet, Line, and Circle methods specify one or more points using this syntax:

(x, y)

You can precede each of these points with the Step keyword, specifying that the location of the point is relative to the last point drawn. Visual Basic adds the x and y values to the values of the last point drawn. For example, the statement:

Line (100, 200)–(150, 250)

is equivalent to:

Line (100, 200)–Step(50, 50)

In many situations, the Step keyword saves you from having to constantly keep track of the last point drawn. Often you may be more interested in the relative position of two points than their absolute position.

Using the Color Argument

To vary the color of the line, use the optional color argument with graphics methods. For example, this statement draws a dark blue line:

Line (500, 500)–(2000, 2000), RGB(0, 0, 255)

If you omit the color argument, the ForeColor property for the object where the line is being drawn determines its color.