Graphics in QBasic


QBasic allows your program to make use of your graphics display monitor and the speaker on your personal computer.

Graphics

QBasic allows you to use the screen on your monitor in several different modes. These modes are identified by numbers. By default, QBasic uses mode 0 for the screen. In mode 0, the screen is a grid of 80 columns and 25 rows. Your program can write to each of these grid locations. The top left corner of the screen is the location (1, 1), i.e., row 1, column 1. The bottom right corner of your screen is (80, 25), i.e., column 80, row 25. Notice that the co-ordinates for a grid location are specified by first writing the column number, and then the row number. In mode 0, you can specify printing characters at grid locations. This does not allow you to make use of the resolution of your monitor. Monitors actually display information by drawing dots on pixels (picture elements). To be able to use a higher resolution for the screen from your programs, you should use either mode 1 or mode 2. The SCREEN statement allows you to change the graphics mode that your program uses. The statement

SCREEN 1
will change the mode of the screen to graphics mode 1. You can change the graphics mode any number of times in your program.

Graphics Mode 1

In graphics mode 1, the resolution of the screen is 320 columns and 200 rows of pixels. The co-ordinates of the top, left corner pixel are (0, 0) and the co-ordinates of the bottom, right corner pixel are (319, 199). Once again, in writing co-ordinates, the column number (or X-coordinate) is written first and then the row number (or Y-co-ordinate). Also, notice that the column and row numbers start at 0.

Graphics Mode 2

In graphics mode2, the resolution of the screen is 640 columns and 200 rows of pixels, giving you twice the number of columns as graphics mode 1. The co-ordinates of the top, left corner pixel are (0, 0) and the co-ordinates of the bottom, right corner pixel are (319, 199).

Drawing in Graphics Modes 1 and 2

QBasic has statements to let you draw basic figures on the screen. When you start a graphics mode with a SCREEN command, there is a default background colour (black) and a default foreground colour (white) assigned to the screen. All that you draw or print from your QBasic program is in the foreground colour. In graphics mode 1, QBasic allows you to choose a colour for your background and a palette of colours for the foreground with the command COLOR. For more information see the description of the command COLOR under Help in QBasic.

Below is a brief description of some of the commands for drawing simple objects such as points, lines, circles, etc. For more information on each of these command, see the description of the command under Help in QBasic.

Drawing Points

The QBasic command to draw a point is PSET. The command

PSET (10, 50)
will draw the point at co-ordinates (10, 50) in the foreground colour. In graphics mode 1, you could specify the colour that the point should be drawn in. The command
PSET (10, 50), 2
will draw the same point, but in the colour whose number is 2 in the current foreground palette.

The command PRESET erasee the point at the specified co-ordinates. Actually, it draws the point in the background colour, hence giving the feeling that the point was erased. For example, the command

PRESET (10, 50)
will "erase" the point at co-ordinates (10, 50).

Drawing Lines

The QBasic command to draw a straight line is LINE. The command
LINE (10, 50)-(40, 80)
will draw the straight line joining the points at co-ordinates (10, 50) and (40, 80) in the foreground colour. The command
LINE (10, 50)-(40, 80), 0
will draw the straight line joining the points at co-ordinates (10, 50) and (40, 80) in the colour 0. This way you can use the colour of the background to "erase" a line.

Drawing Rectangles

The LINE command can also be used to draw unfilled and filled rectangles. The command The command
LINE (10, 50)-(40, 80), , B
will draw an unfilled rectangle whose two diagonally opposite corners are (10, 50) and (40, 80). The last B in the command above, specifies that an unfilled rectangle is to be drawn. To draw the same rectangle filled with the foreground colour, replace the B with a BF in the above command.

Drawing Circles

The QBasic command to draw a circle is CIRCLE. The command
CIRCLE (10, 50), 5
will draw the circle whose center is at the point (10, 50) and whose radius is 5. The command
CIRCLE (10, 50), 5, 2
will draw the same circle as above but in the colour 2.

The CIRCLE command also provides a mechanism for drawing arcs, i.e., not complete circles. To do this, you have to specify the starting angle of the arc from the positive X axis and the ending angle of the arc from the positive X axis. The angles have to be specified in radians.. For example, the command

CIRCLE (10, 50), 5, 2, 0, 3.14
will draw a semicircle in colour 2, above the X axis, with the center at (10, 50) and a radius of 5.

Back to Fall 95 Coursework