Loading....
Coupon Accepted Successfully!

 

Filling Patterns With Different Colors And Styles

Now we will use a graphics function to fill the different patterns with different colors using different styles. In the following program a setfillstyle() function is used for drawing a bar. This function needs to pass two parameters the first being the pattern and second being the color.

22.15 Write a program to use the setfillstyle() and fill up a bar and 3dbar with different colors.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<stdlib.h>

int main()

{

int gd =DETECT,gm;

initgraph(&gd,&gm, “c:\\tc\\bgi”);

setcolor(GREEN);

setfillstyle(SOLID_FILL,RED);

bar(20,30,200,40);

moveto(250,50);

bar(200,50,250,60);

setfillstyle(CLOSE_DOT_FILL,GREEN);

bar3d(150,100,200,200,20,1);

getch();

closegraph();

restorecrtmode();

return 0;

}

Explanation: This program uses the setfillstyle() function for filling various colors with different styles. A bar and a 3-d bar are drawn with two different colors. The programmer can run the program and see its effects.

22.16 Write a program to draw the triband flag and a circle on the middle band. Use setfillstyle(), setbkcolor(), setclolor() functions. Show the flag pattern with colors on the screen.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<stdlib.h>

int main()

{

int gd =DETECT,gm;

initgraph(&gd,&gm, “c:\\tc\\bgi”);

setfillstyle(1,6);

bar(20,30,100,40);

rectangle( 20,30,100,40);

setfillstyle(1,15);

bar(20,40,100,50);

rectangle( 20,40,100,50);

setfillstyle(1,2);

bar(20,50,100,60);

rectangle( 20,50,100,60);

line(20,30,20,150);

setbkcolor(1);

setcolor(2);

setfillstyle(1,3);

circle(60,45,5);

getch();

closegraph();

restorecrtmode();

return 0;

}

Explanation: This program uses the setfillstyle() function for filling various bars with different colors. The background color used is blue with a function setbkcolor(). Bars are drawn with different colors. A circle is drawn on the middle bar. The programmer can run the program and see the output.

22.17 Write a program to draw the rectangles duly filled with different patterns using setfillstyle() and show its effect on the VDU.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<stdlib.h>

 

int main()

{

int gd =DETECT,gm,i,x=40,y=80;

initgraph(&gd,&gm,“c:\\tc\\bgi”);for(i=0;i<11;i++)

{

Setcolor(i);

setfillstyle(i,2);

bar(x,y,x+40,y+40);

rectangle( x,y,x+40,y+40);

x=x+50;

if(x>500)

{

y=y+50;

x=20;

}

getche();

}

closegraph();

restorecrtmode();

return 0;

}

Explanation: This program uses the setfillstyle() function for filling the rectangles with different patterns. Rectangles are drawn with different colors. The programmer can run the program and see the output.

22.18 Write a program to draw boxes using bar() function. Fill the boxes with different designs.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<stdlib.h>

void main()

{

int gd=DETECT,gm,a=40,b=40,ptr,k;

char txt[10];

char *design[]= {“EMPTY_FILL”, “SOLID_FILL”,“LINE_FILL”,

“LTSLAH_FILL”,“SLASH_FILL”,“BKSLASH_FILL”,“LTBKSLASH_FILL”,

“HATCH_FILL”, “XHATCH_FILL”,

“INTERLEAVE_FILL”, “WIDE_DOT_FILL”,

“CLOSE_DOT_FILL”, “USER_FILL” };

 

initgraph(&gd,&gm, “c:\\tc\\bgi”); setcolor(RED);

settextstyle(0,0,2);

outtextxy(100,1,“User-defined Styles”);

settextstyle(0,0,0);

setcolor(WHITE);

for (ptr=0;ptr<12;ptr++)

{

setfillstyle(ptr,ptr);

bar(a,b,a+70,b+70);

rectangle(a,b,a+70,b+70);

k=1+ptr;

itoa(k,txt,10);

outtextxy(a,b+100,txt);

setcolor(ptr+1);

outtextxy(a,b+110,design[ptr]);

a=a+150;

if (a>490)

{

b=b+150;

a=40;

}

}

getche();

closegraph();

restorecrtmode();

}

Explanation: In the above program, the settextstyle() function sets text attributes such as font size, direction of horizontal or vertical text, and size of the character. The message “user-defined styles” is displayed in a larger size with red color. Again, the text attributes are set to 0 so that they will not further affect the outputting text. The character pointer is initialized with pre-defined patterns (designs). The setfillstyle() function has two arguments: patterns and color to be filled. The bar() function draws the bar using an attribute of setfillstyle(). The rectangle() function draws a border for the various bars created by the bar() function. The itoa() function converts an integer into a string. The numbers 1 to 12 displayed below each bar are due to this function.

22.19 Write a program to draw the rectangle floodfill() function and show its effect on the VDU.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<stdlib.h>

 

int main()

{

int gd =DETECT,gm,x,y;

initgraph(&gd,&gm, “c:\\tc\\bgi”); setcolor(WHITE);

outtextxy(60,20,“RECTANGLE”);

setcolor(GREEN);

rectangle(20,40,200,60);

setfillstyle(1,3);

floodfill(21,41,GREEN);

getch();

closegraph();

restorecrtmode();

return 0;

}

Explanation: This program uses setfillstyle() and floodfill() functions for filling the rectangles with any pattern. It is not necessary to use bar() at first and then enclose it by rectangle as used in the previous program. Straightway, one can use floodfill() function for filling a pattern with different colors. The programmer can run the program and see output.





Test Your Skills Now!
Take a Quiz now
Reviewer Name