Computer Graphics Lab 1
Computer Graphics Lab 1
graphics.h: This interface 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.
Functions
initGraphics()
Creates the graphics window on the screen.
initGraphics(width, height)
drawArc(bounds, start, sweep) Draws an elliptical arc inscribed in a
drawArc(x, y, width, height, start, sweep) rectangle.
fillArc(bounds, start, sweep)
Fills a wedge-shaped area of an elliptical arc.
fillArc(x, y, width, height, start, sweep)
drawImage(filename, pt)
drawImage(filename, x, y) Draws the image from the specified file with
drawImage(filename, bounds) its upper left corner at the specified point.
drawImage(filename, x, y, width, height)
Returns the bounds of the image contained in
getImageBounds(filename)
the specified file.
drawLine(p0, p1)
Draws a line connecting the specified points.
drawLine(x0, y0, x1, y1)
drawPolarLine(p0, r, theta) Draws a line of length r in the
drawPolarLine(x0, y0, r, theta) direction theta from the initial point.
drawOval(bounds) Draws the frame of a oval with the specified
drawOval(x, y, width, height) bounds.
fillOval(bounds) Fills the frame of a oval with the specified
fillOval(x, y, width, height) bounds.
drawRect(bounds) Draws the frame of a rectangle with the
drawRect(x, y, width, height) specified bounds.
fillRect(bounds) Fills the frame of a rectangle with the
fillRect(x, y, width, height) specified bounds.
drawPolygon(polygon)
drawPolygon(polygon, pt) Draws the outline of the specified polygon.
drawPolygon(polygon, x, y)
fillPolygon(polygon)
fillPolygon(polygon, pt) Fills the frame of the specified polygon.
fillPolygon(polygon, x, y)
drawString(str, pt) Draws the string str so that its baseline origin
drawString(str, x, y) appears at the specified point.
Returns the width of the string str when
getStringWidth(str)
displayed in the current font.
setFont(font) Sets a new font.
getFont() Returns the current font.
setColor(color) Sets the color used for drawing.
Returns the current color as a string in the
getColor()
form "#rrggbb".
saveGraphicsState() Saves the state of the graphics context.
Restores the graphics state from the most
restoreGraphicsState()
recent call to saveGraphicsState().
Returns the width of the graphics window in
getWindowWidth()
pixels.
Returns the height of the graphics window in
getWindowHeight()
pixels.
Issues a request to update the graphics
repaint()
window.
Pauses for the indicated number of
pause(milliseconds)
milliseconds.
Waits for a mouse click to occur anywhere in
waitForClick()
the window.
setWindowTitle(title) Sets the title of the primary graphics window.
Returns the title of the primary graphics
getWindowTitle()
window.
Closes the graphics window and exits from
exitGraphics() the application without waiting for any
additional user interaction.
Example 1: To set Text Position.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
//outtextxy
int main()
int gd=DETECT,gm,error;
initgraph(&gd,&gm,"c:\\tc\\bgi");
error = graphresult();
if (error != grOk)
getch();
exit(1);
getch();
closegraph();
return 0;
}
Output
Example 2: To set Background Colour.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
//outtextxy
int main()
int gd=DETECT,gm,error;
initgraph(&gd,&gm,"c:\\tc\\bgi");
error = graphresult();
if (error != grOk)
getch();
exit(1);
setbkcolor(BLUE);
getch();
closegraph();
return 0;
}
Output
Example 3: To set text Size.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
int main()
int gd=DETECT,gm,i,j=0,error,maximumcolor;
char message[80];
initgraph(&gd,&gm,"c:\\tc\\bgi");
error = graphresult();
if (error != grOk)
getch();
exit(1);
setbkcolor(BLUE);
outtextxy(1,j, message);
j += textheight(message);
}
getch();
closegraph();
return 0;
Output
Example 4: To draw graphics.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
int main()
int gd=DETECT,gm,i,error;
char message[80];
initgraph(&gd,&gm,":\\tc\\bgi");
error = graphresult();
if (error != grOk)
getch();
exit(1);
setbkcolor(BLUE);
for(i=1;i<=150;i+=10)
cleardevice();
//void arc (int x, int y, int startangle, int endangle, int radius);
outtextxy(getmaxx()-200,getmaxy()-100,"Center:(310,250)");
outtextxy(getmaxx()-200,getmaxy()-80,message);
outtextxy(getmaxx()-200,getmaxy()-40,message);
getch();
cleardevice();
for(i=1;i<=150;i+=10)
getch();
closegraph();
return 0;
OutPut