Q(uick)BASIC Statement: VIEW

Quick View

VIEW

A graphics statement that defines screen limits for graphics output

Worth knowing

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

Syntax
  • VIEW [[SCREEN] (x1!,y1!)-(x2!,y2!) [,[color%] [,border%]]]
Description/Parameter(s)
SCREEN Specifies that coordinates are relative to the screen rather than the viewport.
(x1!,y1!)-(x2!,y2!) The coordinates of diagonally opposite corners of the viewport.
color% A color attribute that sets the viewport fill color.
border% VIEW [[SCREEN] (x1!,y1!)-(x2!,y2!) [,[color%] [,border%]]]
If all arguments are omitted, the entire screen is the viewport.
The available color attributes depend on your graphics adapter and the screen mode set by the most recent SCREEN statement.
Example
'This example requires a color graphics adapter. SCREEN 1 VIEW (10, 10)-(300, 180), , 1 LOCATE 1, 11: PRINT "A big graphics viewport"; VIEW SCREEN (80, 80)-(200, 125), , 1 LOCATE 11, 11: PRINT "A small graphics viewport";
Syntax
  • VIEW [[SCREEN] (x1,y1)-(x2,y2) [,[color][,border]]]
Description/Parameter(s)
Argument Description
SCREEN When SCREEN is used, the x and y coordinates are absolute to the screen, not relative to the border of the physical viewport. Only graphics within the viewport are plotted. When SCREEN is omitted, all points are plotted relative to the viewport (x1 and x2 are added to coordinates before plotting the point).
(x1,y1)-(x2,y2) Indicates a rectangular area on the screen. The placeholders x1, y1, x2, and y2 are numeric expressions that are the coordinates of diagonally opposite corners of the area.
color The color attribute of the color used to fill the area. If you omit color, the area is not filled.
border Any numeric expression in this area draws a line around the viewport if space is available. If you omit border, no border is drawn.

The VIEW statement defines a "physical viewport," or a rectangular section of the screen into which graphics can be mapped. All coordinates used in the statement must be within the physical bounds of the screen.

If VIEW is given with no arguments, the entire screen is defined as the viewport. RUN and SCREEN also define the entire screen as the viewport and disable any viewports defined with VIEW.

Example

You may use multiple VIEW statements. If the newly described viewport is not wholly within the previous viewport, the screen can be re-initialized with the VIEW statement and the new viewport can be defined. If the new viewport is entirely within the previous one, as in the following example, the intermediate VIEW statement is not necessary. This example opens three viewports, each smaller than the previous one. In each case, the points of the line that lie outside the viewport borders are clipped and do not appear on the screen.

SCREEN 1 CLS VIEW 'Make the viewport cover most of the screen. VIEW (10,10) - (300,180),,1 CLS LINE (0,0) - (310,190),1 LOCATE 1,11: PRINT "A big viewport" VIEW SCREEN (50,50)-(250,150),,1 CLS 'Note CLS clears only viewport. LINE (300,0)-(0,199),1 LOCATE 9,9: PRINT "A medium viewport" VIEW SCREEN (80,80)-(200,125),,1 CLS CIRCLE (150,100),20,1 LOCATE 11,9: PRINT "A small viewport"
Syntax
  • VIEW [[SCREEN] (x1!,y1!)-(x2!,y2!) [,[color&] [,border&]]]
Description/Parameter(s)

The VIEW statement defines a graphics viewport, or "clipping region", which is a rectangular area of the screen to which graphics output is limited.

  • When SCREEN is used:
    • The x and y coordinates of subsequent graphics statements are absolute to the screen, not relative to the border of the viewport.
    • Only graphics within the viewport are plotted.
    When SCREEN is omitted:
    • All points are plotted relative to the viewport (x1! and y1! are added to coordinates before plotting the point).
  • (x1!,y1!)-(x2!,y2!) must be within the physical bounds of the screen in the current screen mode.
  • If you omit color&, the viewport area is not filled (it has the same color as the screen background color).
  • If you omit border&, no line is drawn around the viewport.

Usage Notes

  • If VIEW is used with no arguments, the entire screen is defined as the viewport. RUN and SCREEN also define the entire screen as the viewport.
Example

This example uses the VIEW and WINDOW statements to define a graphics viewport and window. The PMAP function is used to convert viewport coordinates to window coordinates. The program generates a fractal that shows a subset of the complex numbers called the "Mandelbrot Set."

DEFINT A-Z 'Default variable type is integer. DECLARE SUB ScreenTest (EM%, CR%, VL%, VR%, VT%, VB%) 'Set maximum number of iterations per point. CONST MAXLOOP = 30, MAXSIZE = 1000000 CONST FALSE = 0, TRUE = NOT FALSE 'Boolean constants. 'Set window paramters. CONST WLeft = -1000, WRight = 250, WTop = 625, WBottom = -625 'Call ScreenTest to find out if this is an EGA machine, 'and get coordinates of viewport corners. ScreenTest EgaMode, ColorRange, VLeft, VRight, VTop, VBottom 'Define viewport and corresponding window. VIEW (VLeft, VTop)-(VRight, VBottom), 0, ColorRange WINDOW (WLeft, WTop)-(WRight, WBottom) LOCATE 24, 10: PRINT "Press any key to quit."; XLength = VRight - VLeft YLength = VBottom - VTop ColorWidth = MAXLOOP \ ColorRange 'Loop through each pixel in viewport and calculate whether or not 'it is in the Mandelbrot Set. FOR Y = 0 TO YLength 'Loop through every line in the viewport. LogicY = PMAP(Y, 3) 'Get the pixel's logical y coordinate. PSET (WLeft, LogicY) 'Plot leftmost pixel in the line. OldColor = 0 'Start with background color. FOR X = 0 TO XLength 'Loop through every pixel in the line. LogicX = PMAP(X, 2) 'Get the pixel's logical x coordinate. MandelX& = LogicX MandelY& = LogicY 'Do the calculations to see if this point is in the Mandelbrot Set. FOR I = 1 TO MAXLOOP RealNum& = MandelX& * MandelX& ImagNum& = MandelY& * MandelY& IF (RealNum& + ImagNum&) >= MAXSIZE THEN EXIT FOR MandelY& = (MandelX& * MandelY&) \ 250 + LogicY MandelX& = (RealNum& - ImagNum&) \ 500 + LogicX NEXT I 'Assign a color to the point. PColor = I \ ColorWidth 'If color has changed, draw a line from the last point 'referenced to the new point, using the old color. IF PColor <> OldColor THEN LINE -(LogicX, LogicY), (ColorRange - OldColor) OldColor = PColor END IF IF INKEY$ <> "" THEN END NEXT X 'Draw the last line segment to the right edge of the viewport. LINE -(LogicX, LogicY), (ColorRange - OldColor) NEXT Y DO LOOP WHILE INKEY$ = "" SCREEN 0, 0 'Restore the screen to text mode, WIDTH 80 '80 columns. END BadScreen: 'Error handler that is invoked if EgaMode = FALSE 'there is no EGA graphics card. RESUME NEXT 'This procedure tests to see if user has EGA hardware with SCREEN 8. 'If this causes an error, the EM flag is set to FALSE, and the screen 'is set with SCREEN 1. The procedure also sets values for the corners 'of the viewport (VL = left, VR = right, VT = top, VB = bottom), 'scaled with the correct aspect ratio so viewport is a perfect square. SUB ScreenTest (EM, CR, VL, VR, VT, VB) STATIC EM = TRUE ON ERROR GOTO BadScreen SCREEN 8, 1 ON ERROR GOTO 0 IF EM THEN 'No error, so SCREEN 8 is OK. VL = 110: VR = 529 VT = 5: VB = 179 CR = 15 '16 colors (0 - 15). ELSE 'Error, so use SCREEN 1. SCREEN 1, 1 VL = 55: VR = 264 VT = 5: VB = 179 CR = 3 '4 colors (0 - 3). END IF END SUB