Q(uick)BASIC Statement: DRAW

Quick View

DRAW

A graphics statement that interprets a string expression and draws an object

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • DRAW commandstring$
Description/Parameter(s)
commandstring$ A string expression that contains one or more of the following DRAW commands:
Line-drawing and cursor-movement commands:
D[n%] Moves cursor down n% units.
E[n%] Moves cursor up and right n% units.
F[n%] Moves cursor down and right n% units.
G[n%] Moves cursor down and left n% units.
H[n%] Moves cursor up and left n% units.
L[n%] Moves cursor left n% units.
M[{+|-}]x%,y% Moves cursor to point x%,y%. If x% is preceded by + or -, moves relative to the current point.
R[n%] Moves cursor right n% units.
U[n%] Moves cursor up n% units.
[B] Optional prefix that moves cursor without drawing.
[N] Optional prefix that draws and returns cursor to its original position.
Color, rotation, and scale commands:
An% Rotates an object n% * 90 degrees (n% can be 0, 1, 2, or 3).
Cn% Sets the drawing color (n% is a color attribute).
Pn1%,n2% Sets the paint fill and border colors of an object (n1% is the fill-color attribute, n2% is the border-color attribute).
Sn% Determines the drawing scale by setting the length of a unit of cursor movement. The default n% is 4, which is equivalent to 1 pixel.
TAn% Turns an angle n% degrees (-360 through 360).
  • If you omit n% from line-drawing and cursor-movement commands, the cursor moves 1 unit.
  • To execute a DRAW command substring from a DRAW command string, use the "X" command:

  • DRAW "X" + VARPTR$(commandstring$)

Differences from BASICA

  • DRAW and PLAY statements in BASICA that use variables or other command strings within command strings must be modified for QBasic. Use the VARPTR$ function, which returns a string representation of the variable's address:
BASICA Statement QBasic Equivalent
DRAW "Xcmdstring$" DRAW "X" + VARPTR$ (cmdstring$)
DRAW "TA = angle" DRAW "TA =" + VARPTR$(angle)
Example

This example requires a color graphics adapter.

SCREEN 1 Triangle$ = "F60 L120 E60" DRAW "C2 X" + VARPTR$(Triangle$) DRAW "BD30 P1,2 C3 M-30,-30"
Syntax
  • DRAW stringexpression
Description/Parameter(s)

The DRAW statement combines many of the capabilities of the other graphics statements into a graphics macro language. This macro language defines a set of characteristics that can be used to describe an image. These characteristics include motion (up, down, left, right), color, rotation angle, and scale factor. The stringexpression consists of these macro commands.

There are three types of macro commands:

Differences from BASICA

  • The DRAW statement requires modification of BASICA programs when used with QuickBASIC. Specifically, the compiler requires the VARPTR$ form for variables. Statements such as the following:

  • DRAW "XA$"
  • DRAW "TA = ANGLE"

  • (where A$ and ANGLE are variables) must be changed as follows:

  • DRAW "X" + VARPTR$(A$)
  • DRAW "TA =" + VARPTR$(ANGLE)

  • when using the compiler.
  • The compiler does not support the Xstringexpression command. However, you may execute a substring by appending the character form of the address to X. For example, the following two statements are equivalent. The first statement works when within the environment and when using the compiler, while the second works only within the environment.

  • DRAW "X" + VARPTR$(A$)
  • DRAW "XA$"
Examples

The following program draws a triangle's outline in magenta and paints the interior cyan:

SCREEN 1 DRAW "C2" 'Set color to magenta. DRAW "F60 L120 E60" 'Draw a triangle. DRAW "BD30" 'Move down into the triangle. DRAW "P1,2" 'Paint interior.

The following example shows how to use the M macro command:• with absolute and relative movement
• with string- and numeric-variable arguments

SCREEN 2 PRINT "Press any key to continue..." 'Absolute movement DRAW "M 50,80" DRAW "M 80,50" LOCATE 2, 30: PRINT "Absolute movement" DO LOOP WHILE INKEY$ = "" 'Relative movement DRAW "M+40,-20" DRAW "M-40,-20" DRAW "M-40,+20" DRAW "M+40,+20" LOCATE 3, 30: PRINT "Relative movement" DO LOOP WHILE INKEY$ = "" 'Using a string variable. X$ = "400": Y$ = "190" DRAW "M" + X$ + "," + Y$ LOCATE 4, 30: PRINT "String variable" DO LOOP WHILE INKEY$ = "" 'Using numeric variables (note the two "=" signs). A = 300: B = 120 DRAW "M=" + VARPTR$(A) + ",=" + VARPTR$(B) LOCATE 5, 30: PRINT "Numeric variables"

DRAW_EX.BAS is a program file in the subdirectory ADVR_EX that illustrates the DRAW statement. To look at the program in the View window and, optionally, to run it, load DRAW_EX.BAS using the File menu's Open Program command.
The program draws a clock on the screen using the TIME$ function.

Syntax
  • DRAW stringexpression$
Description/Parameter(s)
stringexpression$ Contains one or more drawing commands; the drawing commands combine many of the capabilities of the other graphics statements (such as LINE and COLOR) into a graphics macro language.
 
Color, Scale, and Rotation Commands
[B] Move without plotting [N] Plot and return
U[n] Up E[n] Diagonally up and right
D[n] Down F[n] Diagonally down and right
L[n] Left G[n] Diagonally down and left
R[n] Right H[n] Diagonally up and left
M[{+|-}]x,y Move to point x,y (or relative move)
 
Line-Drawing and Cursor-Movement Commands
Cn Change the drawing (foreground) color (SCREEN mode determines valid values for 'n')
Pp,b Fill enclosed shape that has border color 'b' with color 'p'
Sn Increase or decrease length of moves (n=4 default)
An Rotate (n * 90) degrees, where n = 0, 1, 2, or 3
TAn Rotate (0 <= n <= 360) degrees
  • To execute substrings within a DRAW command, use a command of the form:

  • "X" + VARPTR$(string-expression)

  • DRAW commands coded for BASICA must be revised before they will run with BASIC.

Differences from BASICA

  • Some DRAW statements that are allowable in BASICA programs require modification when used with the BASIC compiler. Specifically, the compiler requires the VARPTR$ form for variables. For example, the BASICA statement

  • DRAW "TA = Angle" ' Angle is a variable.

  • must be changed for the BASIC compiler as follows:

  • DRAW "TA =" + VARPTR$(Angle)

  • The BASIC compiler does not support the BASICA Xstringexpression command. However, you can execute a substring by appending the character form of the address to X. For example, the following two statements are equivalent. The first statement works when within the environment and when using the compiler, while the second works only within the QBX environment.

  • DRAW "X" + VARPTR$(A$)
  • DRAW "XA$"
Examples

This example uses the DRAW statement to draw a triangle's outline in magenta and to paint the interior cyan.

SCREEN 1 DRAW "C2" 'Set color to magenta. DRAW "F60 L120 E60" 'Draw a triangle. DRAW "BD30" 'Move down into the triangle. DRAW "P1,2" 'Paint interior.

The following example shows how to use the M macro command with absolute and relative movement, and with string-variable and numeric-variable arguments.

SCREEN 2 PRINT "Press any key to continue..." 'Absolute movement. DRAW "M 50,80" DRAW "M 80,50" LOCATE 2, 30: PRINT "Absolute movement" DO LOOP WHILE INKEY$ = "" 'Relative movement. DRAW "M+40,-20" DRAW "M-40,-20" DRAW "M-40,+20" DRAW "M+40,+20" LOCATE 3, 30: PRINT "Relative movement" DO LOOP WHILE INKEY$ = "" 'Using a string variable. X$ = "400": Y$ = "190" DRAW "M" + X$ + "," + Y$ LOCATE 4, 30: PRINT "String variable" DO LOOP WHILE INKEY$ = "" 'Using numeric variables (note the two "=" signs). A = 300: B = 120 DRAW "M=" + VARPTR$(A) + ",=" + VARPTR$(B) LOCATE 5, 30: PRINT "Numeric variables"

This example draws a clock on the screen using the TIME$ function.

DECLARE SUB Face (Min$) SCREEN 2 '640 x 200 pixel high-resolution graphics screen. DO CLS 'Get string containing minutes value. Min$ = MID$(TIME$, 4, 2) 'Draw clock face. Face Min$ 'Wait until minute changes or a key is pressed. DO 'Print time at top of screen. LOCATE 2, 37 PRINT TIME$ 'Test for a key press. Test$ = INKEY$ LOOP WHILE Min$ = MID$(TIME$, 4, 2) AND Test$ = "" 'End program when a key is pressed. LOOP WHILE Test$ = "" END 'Draw the clock face. SUB Face (Min$) STATIC LOCATE 23, 30 PRINT "Press any key to end" CIRCLE (320, 100), 175 'Convert strings to numbers. Hr = VAL(TIME$) Min = VAL(Min$) 'Convert numbers to angles. Little = 360 - (30 * Hr + Min / 2) Big = 360 - (6 * Min) 'Draw the hands. DRAW "TA=" + VARPTR$(Little) + "NU40" DRAW "TA=" + VARPTR$(Big) + "NU70" END SUB