You are on page 1of 9

Before starting lab/programming You should have!!!

1. Graphics.h Header File

2. Graphics.lib library file

3. Graphics driver (BGI file)

4. At least 640×480 VGA monitor

5. Header File : graphics.h

6. All Graphical Functions are Included in Graphics.h

7. After Including graphics.h Header File [ You can get access graphical functions ]

8. You must Know Following Things before Learning Turbo Graphics

9. InitGraph : Initializes the graphics system.

10.In C Program execution starts with main() similarly Graphics Environment Starts with this function.
11.initgraph() initializes the graphics system by loading a graphics driver from disk then putting the
system into graphics mode

12.As this is Our first topic Under Graphics so it is better not to go in details of Parameters.

Sample Program:

#include<graphics.h> /* header file */


#include<conio.h>

int main()
{
/* the following two lines are the syntax for writing a particular
program in graphics. It's explanation is given after the
program.*/

int gd = DETECT, gm;


initgraph(&gd,&gm,NULL);

setbkcolor(GREEN);
getch();
closegraph();
return 0;
}
The first step in any graphics program is to include graphics.h header file. The graphics.h header file
provides access to a simple graphics library that makes it possible to draw lines, rectangles, ovals,
arcs, polygons, images, and strings on a graphical window.

The second step is initialize the graphics drivers on the computer using initgraph method
of graphics.h library.

SYNTAX : void initgraph(int *graphicsDriver, int *graphicsMode, char *driverDirectoryPath);

Example: – initgraph(&gd, &gm,NULL);

 gd = graphdriver;
 gm = graphmode

It initializes the graphics system by loading the passed graphics driver then changing the system into
graphics mode. It also resets or initializes all graphics settings like color, palette, current position etc, to
their default values. Below is the description of input parameters of initgraph function.

 graphicsDriver : It is a pointer to an integer specifying the graphics driver to be used. It tells the
compiler that what graphics driver to use or to automatically detect the drive. In all our
programs we will use DETECT macro of graphics.h library that instruct compiler for auto
detection of graphics driver.

*graphdriver is the integer that specifies which graphics driver is to be used.


We can give it a value using a constant of the graphics_drivers enum type,
which is defined in graphics.h and listed below.

graphics_drivers constant Numeric value

DETECT 0 (requests auto detect)


CGA 1
MCGA 2
EGA 3
EGA64 4
EGAMONO 5
IBM8514 6
HERCMONO 7
ATT400 8
VGA 9
PC3270 10
 graphicsMode : It is a pointer to an integer that specifies the graphics mode to be used.
If *gdriver is set to DETECT, then initgraph sets *gmode to the highest resolution available for
the detected driver.

 driverDirectoryPath : It specifies the directory path where graphics driver files (BGI files) are
located. If directory path is not provided, then it will search for driver files in current working
directory directory. In all our graphics programs, you have to set path to NULL.

What is BGI?

Borland Graphics Interface (BGI) is a graphics library that is bundled with several Borland compilers
for the DOS operating systems since 1987. The library loads graphic drivers (*.BGI) and vector fonts
(*.CHR) from disk so to provide device independent graphics support to the programmers.

What is closegraph()?

Syntax for closegraph() :

void closegraph (int wid= ALL_WINDOWS);

Description:

closegraph deallocates the memory allocated by the graphics system and then restores the screen to the
mode it was in before calling initgraph.

Return Value: Return value is none.

Windows Version of closegraph() :

In windows version of closegraph, there is an optional parameter called the ‘wid’ which is the window id
(returned by initwindow) of the window that is supposed to be closed.

The wid parameter can also take one of the two constant values given below:

1. CURRENT_WINDOW which closes only the current window or

2. ALL_WINDOWS which by default closes all the open graphic windows.

By closing the current window, current window will no longer exist and we will not be able to give any
further drawing commands until a new window is created or a current window is set by calling set
current window.

Color Description in C
setbkcolor sets the background to the color specified by the color or the number. The
argument color may be a name or a number as given in the table below. (These
symbolic names are defined in graphics.h). These colors can also be used to
set textcolor (color of the text) or filling inside various shapes that you make in your
program. We shall first learn about the color and their values and then we will learn it
via the programs.

Color Numeric Value


BLACK 0
BLUE 1
GREEN 2
CYAN 3
RED 4
MAGENTA 5
BROWN 6
LIGHTGRAY 7
DARKGRAY 8
LIGHTBLUE 9
LIGHTGREEN 10
LIGHTCYAN 11
LIGHTRED 12
LIGHTMAGENTA 13
YELLOW 14
WHITE 15

GRAPHICS FUNCTIONS

1. Putpixel

Purpose:-Putpixel function is to draw the pixel on the screen. Pixel is small dot on the screen.

Syntax:-putpixel(x co-orinate, y co-ordinate,COLOR);

Example: – putpixel(100,100,BLUE);

2. SetbkColor

Purpose:-Setbkcolor function is used to set background color of the screen.

Syntax:-setbkcolor(COLOR);

Example:-setbkcolor(RED);

3. Setlinestyle
Purpose:-setlinestyle function is used to set the current line style, width and pattern

Syntax:-setlinestyle(linestyle, pattern, thickness);

Example:-setlinestyle(SOLID_LINE,1,2);

4. Setcolor

Purpose:-setcolor is to set color of the objects which is to be drawn after this setcolor line.

Syntax:-setcolor(COLOR);

Example:-setcolor(RED);

5. Rectange:-

Purpose:- Rectangle function is used to draw the rectangle on the screen. X1,y1 are the lower left co-
ordinates of the rectangle and the x2,y2 are the upper right co-ordinates of the rectangle.

Syntax:– rectangle(x1,,y1,x2,y2);

Example:– rectangle(100,100,200,200);

6. Textheight

Purpose:-textheight returns the height of a string in pixels.

Syntax:-textheight(STRING);

Example:-i=textheight(“HELLO”);

7. Textwidth

Purpose:-textwidth returns the width of a string in pixels

Syntax:-textwidth(STRING);

Example:-i=textwidth(“HELLO”);

8. Getx

Purpose:-getx returns the current position’s of x o-ordinate

Syntax:-getx();
Example:-x=getx();

9. Gety

Purpose:-gety returns the current position’s of y co-ordinate

Syntax:-gety();

Example:-y=gety();

10. Getmaxx

Purpose:-getmaxxreturns the maximum x co-ordinate on the screen

Syntax:-getmaxx();

Example:-maxx=getmaxx();

11. Getmaxy

Purpose:-getmaxy returns the maximum y co-ordinate on the screen

Syntax:-getmaxy();

Example:-maxy=getmaxy();

12. Line

Purpose:-Line function is used to draw the line on the screen.

Syntax: line(x1,y1,x2,y2);

Example:-line(100,100,200,100);

14. Moveto

Purpose:-moveto function moves current cursor position on the screen

Syntax:-moveto(x co-ordinate, y co-ordinate);

Example:-moveto(getmaxx/2, getmaxy/2);
16. Circle

Purpose: Circle function is used to draw the circle on the screen

Syntax:– circle(x,y,radius);

Example:circle(100,100,50);

17. Cleardevice

Purpose: cleardevice function is used to clear the contents or graphic images on the screen in graphics
mode.

Syntax:cleardevice();

Example:cleardevice();

18. Outtextxy

Purpose: outtextxy function is used to print the text on the screen in graphics mode.

Syntax:outtext(x,y,text);

Example:-outtextxy(100,100,”HELLO”);

19. Sector

Purpose:sector function draws and fills an elliptical pie slice.

Syntax:sector(x, y, starting angle, ending angle, xradius, yradius);

Example:sector(100,100,45 135 100 50);

20. Arc

Purpose:arc draws the arc on the screen, arc is a part of the circle

Syntax:arc(x, y, starting angle, ending angle, radius);

Example:arc( 100,100,90,180,50);

21. Setfillstyle

Purpose: setfillstyle is used to set the color and style to be filled in the object using the flood fill method.
Syntax:stefillstyle(STYLE, COLOR);

Example:setfillstyle(1,RED)

22. Floodfill

Purpose:floodfill function is used to fill the color in the object, object may be circle, rectangle or any
other closed image.

Syntax:floodfill(x,y,boundary color);

Example:floodfill(100,100,BLUE);

23. Ellipse

Purpose:ellipse function is used to draw the ellipse on the screen.

Syntax:ellipse(x, y, starting angle, ending angle, xradius, yradius);

Example:ellipse(100,100,90,200,20,20);

24. Outtext

Purpose:outtext function is used to display the text on the screen, using this function text is display in
the current position.

Syntax:outtext(STRING);

Example:outtex(“HELLO”);

25. Getcolor

Purpose:getcolor returns the current drawing color.

Syntax:getcolor();

Example:intclr = getcolor();
26. Getpixel

Purpose:getpixel gets the color of a specified pixel.

Syntax:getpixel(x,y);

Example: color=getpixel(100,100);

You might also like