Graphics
Tutorial
In this chapter you will learn about basics of C++ Graphics.
Before getting more information about c++ graphics first we starting from the
header file:graphics.h. This header contains many graphics
functions, if you will not declare this header file the graphics function will
not work.
it means you will have to add #include<graphics.h> in header file section in your program.
it means you will have to add #include<graphics.h> in header file section in your program.
Note : the important thing is BGI Error in c++:
BGI Error: Graphics
not initialized (use ‘initgraph’)
|
If you see the above error in your output screen, you have to use
the following code in main() function
int gd=DETECT,gm;
initgraph(&gd,&gm,”c:\\tc\\bgi\\”);
|
We are starting from line() function in C++ graphics:
line() Function
function line is used to draw a line between two specified points.
its format is:
line(int x1, int y1, int x2, int y2);
|
where int x1 and int y1 is
starting point and int x2 and y2 is ending
point of line, it will draw a line from x1, y1 to x2, y2;
Normally the maximum distance of x is 640, and y is 480.
Example to draw a line
include<graphics.h>
include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,”c:\\tc\\bgi\\”);
line(0,0,640,480);
getch();
}
|
Output
linerel()
The linerel function is used to draw a line up to x2 and y2 from
the current position
Format:
linerel(int x2, int y2);
|
The int x2 and int y2 is ending
point value of line,
e.g-
linerel(640,480);
|
C++
Graphics : setcolor()
setcolor function is used to set the
drawing outline color.
format:
setcolor(int color);
|
int color is a value of color or the name of color,
here is the chart of c++ graphic colors:
Color Name
|
Value
|
Background
|
Foreground
|
BLACK
|
0
|
YES
|
YES
|
BLUE
|
1
|
YES
|
YES
|
GREEN
|
2
|
YES
|
YES
|
CYAN
|
3
|
YES
|
YES
|
RED
|
4
|
YES
|
YES
|
MAGENTA
|
5
|
YES
|
YES
|
BROWN
|
6
|
YES
|
YES
|
LIGHTGRAY
|
7
|
YES
|
YES
|
DARKGRAY
|
8
|
NO
|
YES
|
LIGHTBLUE
|
9
|
NO
|
YES
|
LIGHTGREEN
|
10
|
NO
|
YES
|
LIGHTCYAN
|
11
|
NO
|
YES
|
LIGHTRED
|
12
|
NO
|
YES
|
LIGHTMAGENTA
|
13
|
NO
|
YES
|
YELLOW
|
14
|
NO
|
YES
|
WHITE
|
15
|
NO
|
YES
|
For example:
setcolor(14);
line(0,0,640,480);
|
setbkcolor():
The function setbkcolor is used to set the background color.
format:
setbkcolor(int backgroundcolor);
|
For example:
setbkcolor(3);
setcolor(14);
line(0,0,640,480);
|
C++
Graphics : circle()
>
circle function is used to draw a circle.
format:
circle(int x, int y, int radius);
|
Where int x and int y is center
position of circle and int radius is length of radius of
circle.
example:
include<graphics.h>
include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,”c:\\tc\\bgi\\”);
setbkcolor(1);
setcolor(14);
circle(300,250,200);
getch();
}
|
output:
C++
Graphics : ellipse ()
function ellipse is used to draw an ellipse.
Format:
ellipse(int x, int y, int stange, int
endangle, int x radius, int yradius);
|
where:
x,y
Center of ellipse
stangle
starting angle
endangle ending engle
xradius
Lenth of Horizontal radius.
yradius
lenth of Vertical radius.
If stangle=0 and endangle = 360, the ellipse draws a complete
ellipse.
example:
include<graphics.h>
include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,”c:\\tc\\bgi\\”);
setbkcolor(1);
setcolor(14);
ellipse(300,250,0,360,150,30);
getch();
}
|
Output:
You can draw a half ellipse by the following code:
ellipse(300,250,0,180,150,30);
|
fillellipse()
This function draws a ellipse with solid fill.
format:
fillellipse( int x, int y, int xradius, int y
radius);
|
For Example:
fillellipse(300,250,150,30);
|
C++
Graphics : rectangle ()
The function rectangle draws a rectangle shape.
Format:
rectangle (int left, int top, int right, int
bottom);
|
Example:
include<graphics.h>
include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,”c:\\tc\\bgi\\”);
setbkcolor(1);
setcolor(14);
rectangle(200,150,400,300);
getch();
}
|
Output
C++
Graphics : setfillstyle() and floodfill() function
setfillstyle(int pattern, int color) :
This function sets the fill pattern and color. pattern may be
solid empty, linear etc. and value of pattern will be from 0 to 12 number.
eg-
setfillstyle(5,9)
|
the value 5 display thick line pattern( \\\\).
floodfill ():
floodfill fills the bounded area of drawing.
syntax :
floodfill(int x, int y, int border)
|
the border must be color which you set in drawing color.
if x and y will in under the
bounded region the floodfill will fill the inside of bounded
area, and if the x and y will in outside of
bounded area the floodfill will fill outside area.
Graphics example:
include<graphics.h>
include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,”c:\\tc\\bgi\\”);
setbkcolor(1);
setcolor(14);
circle(300,250,200);
setfillstyle(3,4);
floodfill(320,251,11);
getch();
}
|
output
C++
Graphics : Text settextstyle() function
settextstyle() :
this function is used to set the text style and direction.
format:
settextstyle(int font, int direction, int
charsize);
|
where int font is font name or value
of font, int direction is font direction, it may be vertical
or horizontal, int charsize is size of character.
here is the chart of font name, directions and size.
font name :
Value
|
Name
|
Description
|
0
|
DEFAULT_FONT
|
Bit Mapped font
|
1
|
TRIPLEX_FONT
|
Strocked triplex font
|
2
|
SMALL_FONT
|
Strocked small font
|
3
|
SANS_SERIF_FONT
|
Strocked Sans sarif font
|
4
|
GOTHIC_FONT
|
Strocked gothic font
|
font direction : 0
(for left to right)
1(for bottom to top)
charsize:
Value
Description
0
4*4 pixel
1
8*8 pixel
2
16*16 pixel
3
24*24 pixel
...
up to ten times of normal size..
Note :
you have to use outtext or outtextxy function to print the
text.
E.g.-
outtext(*char);
outtextxy(int x, int y, char ch);
|
example:
include<graphics.h>
include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,”c:\\tc\\bgi\\”);
setbkcolor(1);
setcolor(14);
settextstyle(1,0,4);
outtext(“bitavoncpp.com”);
outtextxy(“bitavoncpp.com”);
getch();
}
|
output
bitavoncpp.com
bitavoncpp.com
|
Write
a program in c++ for password
#include<conio.h>
#include<iostream.h>
void main()
{
//coding by Rameshwar Sahu
//http://bitavoncpp.com
clrscr();
char p1,p2,p3,p4,p5,p6,p7;
cout<<"Please enter Your 7 digits password : ";
p1=getch();
cout<<"*";
p2=getch();
cout<<"*";
p3=getch();
cout<<"*";
p4=getch();
cout<<"*";
p5=getch();
cout<<"*";
p6=getch();
cout<<"*";
p7=getch();
cout<<"*";
getch();
if(p1=='b'&&p2=='i'&&p3=='t'&&p4=='a'&&p5=='v'&&p6=='o'&&p7=='n')
{
clrscr();
cout<<"Welcome! password matched";
}
else
{
clrscr();
cout<<"password didnot matched";
}
getch();
}
#include<iostream.h>
void main()
{
//coding by Rameshwar Sahu
//http://bitavoncpp.com
clrscr();
char p1,p2,p3,p4,p5,p6,p7;
cout<<"Please enter Your 7 digits password : ";
p1=getch();
cout<<"*";
p2=getch();
cout<<"*";
p3=getch();
cout<<"*";
p4=getch();
cout<<"*";
p5=getch();
cout<<"*";
p6=getch();
cout<<"*";
p7=getch();
cout<<"*";
getch();
if(p1=='b'&&p2=='i'&&p3=='t'&&p4=='a'&&p5=='v'&&p6=='o'&&p7=='n')
{
clrscr();
cout<<"Welcome! password matched";
}
else
{
clrscr();
cout<<"password didnot matched";
}
getch();
}
No comments:
Post a Comment