DRAWN OBJECTS - A BEGINNING GRAPHICS TUTORIAL

© 2003, Alyce Watson

excerpt from

Mastering Liberty BASIC 3

Home

Drawn Objects

Documenting Code

Tipcorner - Helpfile

Bmpbuttons

Prompt by Brad Moore

Locate Controls

Tips by Dennis McKinney

Demos by Bill Jennings

Review of TheWrap

Integration by Tom Nally

SQLite by Richard Peeters

Help Writing by Jerry Muelver

Index

There are two kinds of graphics available in Liberty BASIC, turtle graphics, and objects. The turtle graphics method employs a drawing pen that moves about the screen, leaving a trail if the pen is down. Objects and turtle graphics can be drawn together, but it is more usual to see one or the other in a program. For more about turtle graphics, see Issue #98.

For any graphics, the first important lesson is that the pen must be in the DOWN position for graphics to display on the screen. The default is UP, so remember to issue the DOWN command to start drawing.

print #1, "down"    'put the pen down
print #1, "up"      'raise the pen, no graphics will be drawn

SIZE

By default, the drawing pen is a single pixel wide. It can be made any size with the SIZE comand. Remember that objects drawn with a larger-sized pen will take up more space than objects drawn with the default 1-pixel-wide pen. To issue the size command:

print #1, "size 10"    'a pen 10 pixels wide

'using variables:
s = 10
print #1, "size ";s

Remember that variables must be placed outside of the quote marks and that blank spaces must be preserved.

FILL and CLS

The graphicbox or graphics window will be filled with color when the FILL command is issued. This will cover any existing graphics. FILL can be a named Liberty BASIC color, or an RGB color. For more, see Issue #100. To clear the screen and erase drawn objects from memory, issue a CLS command.

print #1, "cls"    'clear screen, erase drawn objects
print #1, "fill white"    'cover over drawn objects

col$="green"
print #1, "fill ";col$

print #1, "fill 10 211 156" 'fill with RGB color

r=142:g=217:b=56
print #1, "fill ";r;" ";g;" ";b

COLOR

Objects are drawn with a pen in the COLOR chosen. The default color is black. To change the color at any time in a program, issue a COLOR command. Objects will be drawn in that color until another COLOR command is issued. The color can be a named Liberty BASIC color, or an RGB color. For more, see Issue #100. Examples of the COLOR command:

print #1, "color red"

col$="blue"
print #1, "color ";col$

print #1, "color 100 220 30"

r=130:g=200:b=255
print #1, "color ";r;" ";g;" ";b

BACKCOLOR

Objects can be drawn as outlines, or as objects filled with a color. Filled objects are filled with the current BACKCOLOR. The default BACKCOLOR is white. For more, see Issue #100. BACKCOLOR can be a named Liberty BASIC color, or an RGB color. It will be used to fill drawn objects until another BACKCOLOR command is issued. Examples of the BACKCOLOR command.

print #1, "backcolor red"

col$="blue"
print #1, "backcolor ";col$

print #1, "backcolor 100 220 30"

r=130:g=200:b=255
print #1, "backcolor ";r;" ";g;" ";b

LOCATION

The pen starts at point 0, 0. This is the upper left corner of the graphics area. The x values get larger as you move right and the y values get larger as you move down. You can locate the pen using turtle graphics commands. See Issue #98. It is more common to locate the pen using the PLACE command when drawing objects. This command puts the drawing pen at the x,y location given. The pen does not draw a line from its previous position. It is lifted and placed at the new location. Examples:

print #1, "place 12 58"    'place pen at x=12, y=58

x=37:y=123
print #1, "place ";x;" ";y

It is possible to retrieve the current pen location with the POSXY command. This command will assign the pen's position to the receiver variables specified.

Syntax: print #handle, "POSXY X Y"

As it would appear in a program:

print #1, "posxy xVar yVar"

After this command, the values for the current pen position are contained in the variables xVar and yVar.

OBJECTS

There are several objects that can be drawn. They are:

For all except the line, if the command contains "FILLED", the object will be a solid object, filled with the current BACKCOLOR. It will completely cover all drawn objects beneath it. Objects that aren't "FILLED" are drawn as outlines. Any drawn graphics that already exist on the screen under the new object will still be visible within its borders. Here is a small demo. Notice how the graphics beneath the box show within its borders, while the graphics beneath the boxfilled are covered by the BACKCOLOR.

nomainwin
WindowWidth=300:WindowHeight=240
open "Test" for graphics_nsb_nf as #1
print #1, "trapclose [quit]"
print #1, "down; size 10; color blue"
print #1, "fill cyan; backcolor pink"
print #1, "line 0 100 500 100"
print #1, "color darkpink"
print #1, "place 10 10; box 180 180"
print #1, "place 200 10; boxfilled 280 180"
wait

[quit]
close #1:end

LINE

The LINE command draws a line in the current pen color and size, from the first pair of points to the second pair of points. If the pen is UP, the pen will move, but no line will be drawn.

Syntax: print #handle, "X1 Y1 X2 Y2"

As it would appear in a program:

print #1, "line 10 56 238 122"

x1=33:x2=287
y1=12:y2=311
print #1, "line ";x1;" ";y1;" ";x2;" ";y2

BOX, BOXFILLED

The two BOX commands draw a box from the current pen position to the x,y position given. BOX draws an outline of a box in the current pen color and size. BOXFILLED draws a box using the current pen size and color and fills it with the current BACKCOLOR.

Syntax: print #handle, "BOX X Y"

As it would appear in a program:

print #1, "box 200 300"
print #1, "boxfilled 145 222"

x=87:y=225
print #1, "box ";x;" ";y


print #1, "boxfilled ";x;" ";y

CIRCLE, CIRCLEFILLED

The two CIRCLE commands draw a circle from the current pen position with the radius given. The radius specifies the distance in pixels from the center of the circle to its outside edge. CIRCLE draws an outline of a circle in the current pen color and size. CIRCLEFILLED draws a circle using the current pen size and color and fills it with the current BACKCOLOR.

Syntax: print #handle, "CIRCLE R"

As it would appear in a program:

print #1, "circle 50"
print #1, "circlefilled 73"

r=100
print #1, "circle ";r
print #1, "circlefilled ";r

ELLIPSE, ELLIPSEFILLED

The two ELLIPSE commands draw an ellipse centered at the current pen position with the width and height given. ELLIPSE draws an outline of an ellipse in the current pen color and size. ELLIPSEFILLED draws an ellipse using the current pen size and color and fills it with the current BACKCOLOR.

Syntax: print #handle, "ELLIPSE W H"

As it would appear in a program:

print #1, "ellipse 200 300"
print #1, "ellipsefilled 145 222"

x=87:y=225
print #1, "ellipse ";x;" ";y
print #1, "ellipsefilled ";x;" ";y

PIE, PIEFILLED

The two PIE commands draw a pie section inside of an ellipse whose width and height are given. The starting and stopping points of the ellipse are expressed in angles. The PIE will start at the first angle and sweep clockwise to the second angle, if the second angle is positive. It will sweep counter-clockwise to the second angle if the second angle is negative. The figure can appear to be any section of a pie: a thin slice, a quarter, half or three-quarters of a pie, or even an entire pie ellipse, if the two angles are equal.

Syntax: print #handle, "PIE W H A1 A2"

As it would appear in a program:

print #1, "pie 300 200 45 125"
print #1, "piefilled 160 220 90 270"

w=150:h=200
a1=75:a2=350
print #1, "pie ";w;" ";h;" ";a1;" ";a2
print #1, "piefilled ";w;" ";h;" ";a1;" ";a2

Here is a small pie demo:

nomainwin
WindowWidth=300:WindowHeight=240
open "Test" for graphics_nsb_nf as #1
print #1, "trapclose [quit]"
print #1, "down; size 5; color darkgreen"

print #1, "fill yellow; backcolor lightgray"
print #1, "place 100 80"
print #1, "pie 120 60 45 125"
print #1, "place 200 120"
print #1, "piefilled 60 160 90 270"
wait

[quit]
close #1:end

Home

Drawn Objects

Documenting Code

Tipcorner - Helpfile

Bmpbuttons

Prompt by Brad Moore

Locate Controls

Tips by Dennis McKinney

Demos by Bill Jennings

Review of TheWrap

Integration by Tom Nally

SQLite by Richard Peeters

Help Writing by Jerry Muelver

Index