Borland Graphics
Borland Graphics
Graphics Setup
There are five steps that you need to follow to use graphics in Turbo C++ 3.0 DOS:
1. Tell the compiler that graphics commands will be used. 2. Have C++ find out what kind of graphics card your computer uses. 3. Initialize the Graphics Screen 4. Tell the system where to find Borlands Graphics routines. 5. Close the graphics screen after you have finished drawing your graphics.
2/21/2012
Graphics Setup 2
1) To tell the compiler that graphics commands will be used, include the preprocessor directive: #include <graphics.h> 2) To have C++ find out what kind of graphics card your computer uses.
declare two variables of type Integer int graphdriver = DETECT, graphmode; I prefer that you use the following command: int graphdriver = VGA, graphmode= VGAHI;
2/21/2012 Mr. Dave Clausen 4
Graphics Setup 3
3 & 4) To initialize the graphics screen and tell the system where to find Borlands Graphics routines (BGI) use the following command in the int main( ) function:
initgraph(&graphdriver, &graphmode, C:\\bc5\\bgi);
After you are finished drawing, you need to use the getch( ); command to leave the picture on the screen (Press any key to continue...). This requires: #include <conio.h> 5) Then close the graphics screen, using: closegraph( );
2/21/2012 Mr. Dave Clausen 5
Fundamentals of Graphics
The Graphics Screen. Color Options. Graphics Mode. Drawing Lines Line Style Clearing the Screen. Plotting Points.
2/21/2012 Mr. Dave Clausen 6
(0, 479)
2/21/2012 Mr. Dave Clausen
(639, 479)
8
Color Options
The number of colors depend on the graphics mode you select using Turbo C++.
The default settings allow for 16 color choices.
You select a foreground or drawing color by using the following command: setcolor(number);
Where (number) is a numeric constant from 0 through 15, or the symbolic constant that represents the color. graphinfo.cpp
2/21/2012 Mr. Dave Clausen 10
Color Names
Here are the color numbers and names:
0 = BLACK 1 = BLUE 2 = GREEN 3 = CYAN 4 = RED 5 = MAGENTA 6 = BROWN 7 = LIGHTGRAY
2/21/2012
Drawing Lines
The Current Pointer. The current pointer is an invisible pointer that keeps track of the current pixel position. It is the equivalent of the visible cursor in text mode.
2/21/2012
12
Drawing Lines 2
To move the pointer to a location on the graph without drawing anything, use the command: moveto (X,Y);
This is like PenUp (PU) in LOGO
To draw lines from the current pointers position to another point on the graph, use the command: lineto (X,Y);
This is like PenDown (PD) in LOGO or SetXY (x, y)
grtmplte.cpp
2/21/2012 Mr. Dave Clausen 13
2/21/2012
14
Rectangles
Rectangles can be drawn in different ways using lineto, moveto, moverel, and linerel. But an easier and faster way is using the Rectangle procedure which draws a rectangle in the default color and line style with the upper left at X1, Y1 and lower right X2, Y2. rectangle (x1, y1, x2, y2);
2/21/2012
16
Circles
Circles can be drawn using the circle procedure. This draws a circle in the default color and line style with center at X, Y, radius in the X direction of Xradius, and corresponding Y radius. circle (x, y, radius);
2/21/2012
17
Arcs
This procedure draws a circular arc in the default color and line style based upon a circle with center X, Y and given X radius. The arc begins at an angle of StartAngle and follows the circle to EndAngle. The angles are measured in degrees from 0 to 360 counterclockwise where 0 degrees is directly right. arc ( x, y, startangle, endangle, radius);
2/21/2012 Mr. Dave Clausen 18
180
270
2/21/2012 Mr. Dave Clausen 19
Ellipses
Draws an elliptical arc in the default color and line style based upon an ellipse with center X, Y and given radii. The arc begins at an angle to Start Angle and follows the ellipse to End Angle. The angles are measured in degrees from 0 to 360 counterclockwise where 0 degrees is directly right.
ellipse ( x, y, startangle , endangle, x_radius, y_radius);
2/21/2012
20
Plotting Points
The Maximum value for X can be found using: getmaxx( ) The Maximum value for Y can be found using: getmaxy( ) To Plot a point: putpixel ( x_value, y_value, color);
Sample Program
Lets look at a program with a line, rectangle, circle, arc, ellipse, and a point. Objects.cpp
2/21/2012
22
Line Style
Setting the line style. All lines have a default line mode, but Turbo C++ allows the user to specify three characteristics of a line: style, pattern, and thickness. Use the command:
setlinestyle (style, pattern, thickness);
2/21/2012
23
24
2/21/2012
25
Filling Patterns
2/21/2012
26
2/21/2012
27
Pattern Names
Here are the name of available patterns:
Values Causing filling with EMPTY_FILL Background Color SOLID_FILL Solid Color LINE_FILL Horizontal Lines LTSLASH_FILL Thin diagonal lines SLASH_FILL Thick diagonal lines BKSLASH_FILL Thick diagonal backslashes LTBKSLASH_FILL Light backslashes HATCH_FILLThin cross hatching XHATCH_FILL Thick cross hatching INTERLEAVE_FILL Interleaving lines WIDE_DOT_FILL Widely spaced dots CLOSE_DOT_FILL Closely spaced dots
2/21/2012 Mr. Dave Clausen 28
Filling Regions
After selecting a color and pattern, floodfill is used to fill the desired area. floodfill ( x, y, border_color ); This paints out the desired color until it reaches border color. Note: The border color must be the same color as the color used to draw the shape. Also, you can only fill completely closed shapes. Program10_4.cpp
2/21/2012 Mr. Dave Clausen 29
To write a literal expression on the graphics screen using the location specified by (x, y) use the command: outtextxy (x, y, literal expression); outtextxy (x, y, string_variable.c_str( ));
Note: string_variable represents a C-style string. When using an apstring variable use the c_str( ) member function to convert the string.
2/21/2012 Mr. Dave Clausen 31
utils.cpp
2/21/2012 Mr. Dave Clausen 32
Text Styles
To set the values for the text characteristics, use: settextstyle ( font, direction, charsize);
Font DEFAULT_FONT TRIPLEX_FONT SMALL_FONT SANS_SERIF_FONT GOTHIC_FONT SCRIPT_FONT SIMPLEX_FONT TRIPLEX_SCR_FONT
2/21/2012
Direction HORIZ_DIR = Left to right VERT_DIR = Bottom to top Fonts continued COMPLEX_FONT EUROPEAN_FONT BOLD_FONT
33
Text Justification
To set the way that text is located around the point specified use the command: settextjustify (horizontal,vertical); Horizontal Vertical LEFT_TEXT TOP_TEXT CENTER_TEXT BOTTOM_TEXT RIGHT_TEXT Program10_2.cpp
2/21/2012 Mr. Dave Clausen 35
2/21/2012
36
Getting a Pixel
To return the color number corresponding to the color located at the point: X, Y use the command: getpixel (x, y);
2/21/2012
38
kbhit()
checks to see if a keystroke is currently available If a keystroke is available, returns a nonzero integer. If a keystroke is not available, returns a zero.
2/21/2012
2/21/2012
40