Drawing Graphics With the Pic Routines Pic.FileNew (filename

advertisement
Drawing Graphics With the Pic Routines
Turing comes with a series of predefined functions and predefined procedures which are used
to draw graphics on the screen. For our purposes, you can think of predefined procedures as
being similar to Turing commands except they have parameters within brackets. Parameters can
be thought of as inputs for the predefined procedure. All of the draw predefined procedures have
parameters (ie. when you type drawdot (130, 200, red), the 130, 200 and red are the parameters).
Predefined functions are similar except that they must be preceded by a command or assigned to
a variable. The most common predefined procedures are listed below.
Pic.FileNew (filename : string) : int
- A predefined function used to load a graphic in GIF, BMP or JPEG format from a file into
memory.
- This must be done before a graphic is used in a program.
- An integer variable which identifies the graphic for the computer must be declared.
Pic.Draw (picID, x, y, mode : int)
- A predefined procedure which is used to draw a graphic on the screen. The picture is drawn
with the lower left corner at coordinate x, y.
- Study the diagram to the left. The top graphic is an illustration of a
graphic on the screen and a new graphic to be placed on the screen.
The three possible mode parameters are explained below and
illustrated in the diagram:
* picCopy draws the picture on top of what was underneath,
obscuring it
* picMerge draws the picture like picCopy except that any
occurrence of the background color in the picture is not drawn to
the screen. This allows you to draw an irregularly-shaped object
and draw it to the screen.
* picUnderMerge draws the picture, but only where the background
color was displayed underneath it. The effect of this is to make the
picture appear to be displayed behind the background.
Pic.Width (picID : int) : int
- A predefined function which returns a picture’s width in pixels
Pic.Height (picID : int) : int
- A predefined function which returns a picture’s height in pixels
Pic.SetTransparentColour (picID, color : int)
- A predefined procedure that will set transparent colour for picMerge draw
Learning Exercise #1 - Pic.FileNew, Pic.Draw, Pic.Height, Pic.Width
Required: 1) Copy the graphic files spaceship.bmp, chair.gif and airplane.bmp from the teacher’s
sharing folder.
2) Input the following program to learn about the various predefined procedures and
functions.
var spaceship, chair: int % Declares variables to store the spaceship and the chair graphics
spaceship := Pic.FileNew ("spaceship.bmp") % Creates a storage spot for the picture of the spaceship
chair := Pic.FileNew ("chair.gif") % Creates a storage spot for the picture of the chair
locatexy (0, 20) % Locates the cursor for the next put statement using x and y coordinates (0,20 in this case)
put "The width of the spaceship graphic is ", Pic.W idth (spaceship), " pixels."
put "The height of the spaceship graphic is ", Pic.Height (spaceship), " pixels."
locatexy (50, 280) % Locates the cursor for the next put statement using x and y coordinates (50, 280 in this case)
put "Example #1"
% The Pic.Draw predefined procedure draws the pictures in the Run Window
% The chair is covering the spaceship with no transparency.
Pic.Draw (spaceship, 50, 300, picCopy)
Pic.Draw (chair, 50, 300, picCopy)
locatexy (250, 280)
put "Example #2"
% Why is the spaceship on top of the chair with no transparency?
Pic.Draw (chair, 250, 300, picCopy)
Pic.Draw (spaceship, 250, 300, picCopy)
locatexy (50, 130)
put "Example #3"
% The spaceship is covering the chair with transparency. What is the difference between this and Example #2?
Pic.Draw (chair, 50, 100, picCopy)
Pic.Draw (spaceship, 50, 100, picMerge)
locatexy (250, 80)
put "Example #4"
% The chair is covering the spaceship with transparency. What is the difference between this and Example #3?
Pic.Draw (chair, 250, 100, picCopy)
Pic.Draw (spaceship, 250, 100, picUnderM erge)
Learning Exercise #2 - Pic.SetTransparentColour
Add the following code to your program. Note you will need to declare a new variable.
locatexy (450, 280)
put "Example #5"
% Note that the background colour of the airplane graphic is red.
airplane := Pic.FileNew ("airplane.bmp") % Creates a storage spot for the picture of the airplane
Pic.Draw (chair, 450, 300, picCopy)
Pic.Draw (airplane, 450, 300, picM erge)
locatexy (450, 80)
put "Example #6"
% Note that the background colour of the airplane graphic is red.
airplane := Pic.FileNew ("airplane.bmp") % Re-creates a storage spot for the picture of the airplane
Pic.SetTransparentColour (airplane, brightred) /*Changes the transparency colour for the airplane graphic to
bright red from white */
Pic.Draw (chair, 450, 100, picCopy)
Pic.Draw (airplane, 450, 100, picM erge)
Download