Professional Documents
Culture Documents
7. After Including graphics.h Header File [ You can get access graphical functions ]
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:
int main()
{
/* the following two lines are the syntax for writing a particular
program in graphics. It's explanation is given after the
program.*/
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.
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.
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()?
Description:
closegraph deallocates the memory allocated by the graphics system and then restores the screen to the
mode it was in before calling initgraph.
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:
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.
GRAPHICS FUNCTIONS
1. Putpixel
Purpose:-Putpixel function is to draw the pixel on the screen. Pixel is small dot on the screen.
Example: – putpixel(100,100,BLUE);
2. SetbkColor
Syntax:-setbkcolor(COLOR);
Example:-setbkcolor(RED);
3. Setlinestyle
Purpose:-setlinestyle function is used to set the current line style, width and pattern
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
Syntax:-textheight(STRING);
Example:-i=textheight(“HELLO”);
7. Textwidth
Syntax:-textwidth(STRING);
Example:-i=textwidth(“HELLO”);
8. Getx
Syntax:-getx();
Example:-x=getx();
9. Gety
Syntax:-gety();
Example:-y=gety();
10. Getmaxx
Syntax:-getmaxx();
Example:-maxx=getmaxx();
11. Getmaxy
Syntax:-getmaxy();
Example:-maxy=getmaxy();
12. Line
Syntax: line(x1,y1,x2,y2);
Example:-line(100,100,200,100);
14. Moveto
Example:-moveto(getmaxx/2, getmaxy/2);
16. Circle
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
20. Arc
Purpose:arc draws the arc on the screen, arc is a part of the circle
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
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
Syntax:getcolor();
Example:intclr = getcolor();
26. Getpixel
Syntax:getpixel(x,y);
Example: color=getpixel(100,100);