2.0 Intro To Small Basic Graphics
2.0 Intro To Small Basic Graphics
GRAPHICS TIME!
SmallBasic has two ways of displaying output; a text mode and a graphics mode.
Graphics mode can display both characters and graphics such as lines, boxes and
other shapes.
You’re working screen has the following layout, which is very much like a graph:
where the default is set to width = 624 and height = 444. We can easily change
these dimensions to our own choices by typing the following lines:
GraphicsWindow.Width = 400
GraphicsWindow.Height = 400
100
50
300
300
GraphicsWindow.DrawLine(x1,y1,x2,y2)
Where
x1 = the position along the x-axis
y1 = the position along the y-axis
x2 = the position along the x-axis
y2 = the position along the y-axis
GraphicsWindow.DrawLine(10,200,200,200)
The PenColor property will change the line color of the item you are drawing. The
PenWidth property will change the border/line width of the item you are drawing.
GraphicsWindow.PenColor = "blue"
GraphicsWindow.PenWidth = 10
GraphicsWindow.DrawLine(10,200,200,200)
Now that we’ve learned the basics of how to draw lines, lets look a bit closer at
shapes. SmallBasic has commands that draw boxes and ovals.
Rectangle
The command is
GraphicsWindow.DrawRectangle(x,y,width,height)
where
x = the top left corner position along the x-axis
y = the top left corner position along the y-axis
width = determines how wide the rectangle will be
height = determines how high the rectangle will be
Oval
The command is
GraphicsWindow.DrawEllipse(x,y,width,height)
where
x = the top left corner position along the x-axis
y = the top left corner position along the y-axis
width = determines how wide the oval will be
height = determines how high the oval will be
Triangle
The command is
GraphicsWindow.DrawTriangle(x1,y1,x2,y2,x3,y3)
where
x1 = the x co-ordinate of the first point.
y1 = the y co-ordinate of the first point.
x2 = the x co-ordinate of the second point.
y2 = the y co-ordinate of the second point.
x3 = the x co-ordinate of the third point.
y3 = the y co-ordinate of the third point.
Activity:
The BrushColor property allows you to fill a shape with a color. Try the
following:
GraphicsWindow.BrushColor = "blue"
When wanting to fill a shape you have to use the BrushColor property and then one
of the following fill statements:
GraphicsWindow.FillRectangle(x,y,width,height)
GraphicsWindow.FillEllipse(x,y,width,height)
GraphicsWindow.FillTriangle(x1,y1,x2,y2,x3,y3)
Colours in SmallBasic
There are several ways to create color in SmallBasic. The following will all output
the same colour.
GraphicsWindow.BrushColor = "blue"
GraphicsWindow.BrushColor = "#0000FF"
GraphicsWindow.BrushColor = GraphicsWindow.GetColorFromRGB(0,0,255)
The following link will provide you with many color names and their related HEX
codes.
Link → COLOURS
The following chart may also help you with some hex codes for the colours.
Drawing Font:
The font used to draw text in the graphics window is specified by four different
properties:
GraphicsWindow.FontName
GraphicsWindow.FontSize
GraphicsWindow.FontBold
GraphicsWindow.FontItalic
● The FontName property is the name of the font, or font face. The default
value is “Tahoma”. Other font names can be found by opening a word
processor and selecting the change font option.
● The FontSize property defines the size of the font, where the default is 12.
● FontBold can have one of two values. If “true”, the font will be bold. If
“false”, it will not be bold. The default value is “true”.
● FontItalic indicates if a font is italicized. The default value is “false” – no
italics.
Try the following code. What are all the properties in the following code doing?
GraphicsWindow.Show()
GraphicsWindow.Title="Font Styles"
GraphicsWindow.Width=400
GraphicsWindow.Height=150
GraphicsWindow.BackgroundColor="Yellow"
GraphicsWindow.FontName = "Cooper Black"
GraphicsWindow.FontSize=36
GraphicsWindow.FontBold="true"
GraphicsWindow.FontItalic="true"
GraphicsWindow.BrushColor="Black"
GraphicsWindow.DrawText(20,40,"Graphics Window")
Activity: Recreate your house, but add colour and text. Attempt other class
appropriate drawings to practice your coordinates.
CHALLENGE:
To make a shape move you need to think about your intended start and end point.
For example if I want a box to move horizontal from x = 100 to x = 300 I could
create the following program.
For x = 1 to 200
GraphicsWindow.DrawRectangle(100+x, 190, 20, 20)
endfor
If you wanted your shape to move up and down which number would you change?
What would you change if you wanted it to grow bigger?
Try inputting the following code. What is each line of code doing?
We have learned how to draw our own images using draw functions but in many
cases we would want to include a saved image
When importing an image make sure it is saved in the same folder as your
SmallBasic file. If this is the care you can use “Program.Directory” in place of the
entire directory link. You MUST use this so that your programs work when you
submit them to your teacher.
First save the image in a variable. In this case I created a variable called “Image”.
This variable is equal to the program directory.
Image = Program.Directory+"image.jpg"
Second there are two ways to put the image on the screen. Using just
“DrawImage” will place the image at the specified location, and maintain the size
of the image.
GraphicsWindow.DrawImage(ImageName,x,y)
Example:
GraphicsWindow.DrawImage(Image,100,100)
GraphicsWindow.DrawResizedImage(ImageName,x,y,width,height)
Example:
GraphicsWindow.DrawResizedImage(Image,100,100,10,10)
GraphicsWindow.DrawResizedImage(image,20,100,50,50)
Shapes
Although we have now learned how to draw and move shapes, there is a separate
way to complete these tasks without using loops and without seeing the glitchy
result.
SmallBasic has an object called “Shapes” which allows you to apply several
modifications to the image including adding, moving, rotating and zooming.
Keep in mind that shapes still work in layers. Layers are created in the same order
your program is coded.
Creating Shapes
GraphicsWindow.BrushColor = "green"
Rectangle = Shapes.AddRectangle(100,100)
GraphicsWindow.BrushColor = "yellow"
Circle = Shapes.AddEllipse(50,50)
Moving Shapes
GraphicsWindow.BrushColor = "green"
Rectangle = Shapes.AddRectangle(100,100)
GraphicsWindow.BrushColor = "yellow"
Circle = Shapes.AddEllipse(50,50)
Shapes.Move(Rectangle, 200,200)
Shapes.Move(Circle, 300,200)
Animating Shapes
GraphicsWindow.BrushColor = "green"
Rectangle = Shapes.AddRectangle(100,100)
For counter = 1 To 5
Program.Delay(1000)
Shapes.HideShape(Rectangle)
Program.Delay(1000)
Shapes.ShowShape(Rectangle)
EndFor
Rotating
'creates rectangle
GraphicsWindow.BrushColor = "Purple"
rotateshape = Shapes.AddRectangle(150, 100)
Shapes.Move(rotateshape, 200, 150)
Shapes.SetOpacity(ShapeName, level)
● Level range is 0 for completely transparent to 100 for solid
For i = 1 To 4
Program.Delay(1000)