CG-4
CG-4
Arshad Khan
Bakhtar Institute of Higher Education
Initializing Graphics Mode
The computer display system must be
initialized into graphics mode before calling
graphics function.
The “initgraph” function is used to initialize
the display into graphics mode.
This function is a part of “graphics.h” header
file. So this file is included in the program
before executing “initgraph” function.
The sysntax of “initgraph” function is :
Initgraph(&driver, &mode, “path”);
driverrepresent the graphic driver in the
computer. The graphic driver can be detect
atomically by using the Keyword DETECT.
If the driver is to be automatically detected,
the variable driver is declared as of integer
type and detect value is assigned to it as.
int driver, mode;
driver=DETECT;
This statement must be placed before “initgraph” function.
When the above statement is executed, the computer automatically
detects the graphic driver and the graphics mode.
If the driver is auto detected then the use of mode is option. Normally
mode represent the output resolution on the computer screen.
A Simple Program
#include<graphics.h>
#include<conio.h>
int main()
{
int d,m;
d=DETECT;
initgraph(&d, &m,”C:\\Dev-C++\\BGI”);
getch();
return 0;}
The “cleardevice()” Function
The cleardevice function is used to clear the screen in
graphics mode. It is similar to clrscr() function in text
mode.
The Closing Graphics Mode:
The “The closegraph()” function is used to restore the
screen to the text mode.
When the graphic mode is initialized, memory is
allocated to graphic sys. When closegraph function is
executed, it de-allocate all memory to the graphic sys.
Normally used at the end of program. closegraph();
Text in graphics mode
text can be written in different fonts, styles,
size, color and direction.
The “outtext” Function: the outtext function is
used to print text on computer screen in
graphics mode.
General syntax is
outtext(string)
String the characters that is to be printed on the
screen.
Write Text
#include<graphics.h>
#include<conio.h>
void main()
{ int d,m;
d=DETECT;
initgraph(&d,&m,"C:\\TC\\BGI");
cleardevice();
outtext("I Love Afghanistan");
getch();
closegraph(); }
moveto Function
The “moveto” function is used to move the
current cursor position to a specified
location on the screen similar like gotoxy
function in text mode.
General form is: moveto(x,y);
x represents the x-coordinate of the screen. It is the
horizontal distance in pixels from the left of the screen.it
may be unsigned int type value or variables. its value is
from 0 to 639. y represents the y-coordinate of the
screen. It is the vertical distance in pixels from the top of
the screen. it may be unsigned int type value or
variables. its value is from 0 to 479.
Simple Program
#include<graphics.h>
#include<conio.h>
int main()
{
int d,m;
d=DETECT;
initgraph(&d,&m,"");
moveto(115,120);
outtext("Wellcome To");
moveto(100,150);
outtext(“Bakhtar University");
circle(150,150,100);
circle(150,150,110);
getch();
cleardevice();
return 0;
}
outtextxy() Function
The function is similar to the outtext()
function but it is used to print the text on
the screen at a specified location.
Instead of moveto and outtext function we
can use outtextxy function.
General form is:
outtextxy(x,y,string);
The previous program.
#include<graphics.h>
#include<conio.h>
int main()
{
int d,m;
d=DETECT;
initgraph(&d,&m,"");
outtextxy(115,120,"Wellcome To");
outtextxy(100,150,“BakhtarUniversity");
circle(150,150,100);
circle(150,150,110);
getch();
cleardevice();
return 0;
}
The settextstyle() function
This function is used to define the text style in graphics
mode.
Text style include the font type, font size and text
direction
Syntax settextstyle(style, dir, size);
Where
Style: specified the font style (rang: 0-10) or 0-15
Dir: the direction of the text
0 (HORIZ_DIR) for horizontal direction &
1(VERT_DIR) for vertical direction
Size: specified the font size of the text, its values
is from 0 to 72
#include<graphics.h>
#include<conio.h>
int main()
{
int d,m;
d=DETECT;
initgraph(&d,&m,"");
cleardevice();
settextstyle(11,VERT_DIR,0); //0R settextstyle(11,1,0)
moveto(150,150);
outtext("I LOVE AFGHANISTAN");
getch();
closegraph();
return 0; }
Program to display text in different style.
#include <graphics.h>
#include <conio.h>
main()
{
int d=DETECT,m,x=25,y=25,f=0;
initgraph(&d,&m,"");
for(;f<=10;f++)
{
settextstyle(f,HORIZ_DIR,2);
outtextxy(x,y,"I LOVE MY COUNTRY");
y=y+30;
}
getch();
closegraph();
}
Program to display text in
different style.
#include <graphics.h>
#include<conio.h>
int main()
{
int d,m;
d=DETECT;
initgraph(&d,&m,"");
cleardevice();
for(int i=0;i<=5;i++)
{
settextstyle(i,0,0);
outtextxy(100,10+i*30,"I LOVE AFGHANISTAN");
}
getch();
closegraph();
return 0;
}
Q&A
Session