Parameters
| Name | Type | Description |
|---|---|---|
pattern | int | An integer color pattern index or one of the pre-defined BGI fill pattern constants. Valid ranges are 0 through 12. |
color | int | The fill color index (typically 0 to 15) or a standard BGI color name constant (e.g. RED, YELLOW). |
Both parameters are integer values. The pattern constants are defined in graphics.h as enumerations. Below is the complete catalog of BGI fill patterns available for use.
BGI Fill Pattern Table
| Index | Constant Name | Description |
|---|---|---|
0 | EMPTY_FILL | Fills with the current background color (solid erase). |
1 | SOLID_FILL | Fills with a solid, uniform color (most commonly used). |
2 | LINE_FILL | Fills with horizontal parallel lines. |
3 | LTSLASH_FILL | Fills with light slanted lines (slanted top-left to bottom-right). |
4 | SLASH_FILL | Fills with thick slanted lines (slanted top-left to bottom-right). |
5 | BKSLASH_FILL | Fills with thick back-slanted lines (top-right to bottom-left). |
6 | LTBKSLASH_FILL | Fills with light back-slanted lines (top-right to bottom-left). |
7 | HATCH_FILL | Fills with a horizontal/vertical grid crosshatch pattern. |
8 | XHATCH_FILL | Fills with a diagonal crosshatch pattern. |
9 | INTERLEAVE_FILL | Fills with interleaved parallel line segments. |
10 | WIDE_DOT_FILL | Fills with widely spaced sparse dots. |
11 | CLOSE_DOT_FILL | Fills with closely spaced dense dots. |
12 | USER_FILL | Fills with a custom 8x8 pattern defined using setfillpattern(). |
Return Value
setfillstyle() returns void. If you need to retrieve the current fill settings, call the function getfillsettings(), which populates a structure with the active pattern index and color value.
Code Examples
All 12 Fill Patterns Demo
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main() {
int gd = DETECT, gm;
int i, x = 30, y = 50;
char text[15];
initgraph(&gd, &gm, "");
for(i = 1; i <= 11; i++) {
// Apply fill pattern i with color light blue (9)
setfillstyle(i, 9);
// Draw a filled bar
bar(x, y, x + 80, y + 80);
// Outline the bar in white
setcolor(WHITE);
rectangle(x, y, x + 80, y + 80);
// Label the pattern index
sprintf(text, "Pattern: %d", i);
outtextxy(x, y + 90, text);
x += 120;
if(x > 500) {
x = 30;
y += 150;
}
}
getch();
closegraph();
return 0;
}
This program loops from index 1 (SOLID_FILL) through index 11 (CLOSE_DOT_FILL). For each index, it configures `setfillstyle()`, draws a filled bar, and outlines it with a white rectangle. This visual grid acts as a useful reference index sheet for fill styles.
A simple textured house sketch
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
int points[] = {150, 200, 250, 100, 350, 200, 150, 200};
initgraph(&gd, &gm, "");
// 1. Draw solid yellow sky background
setfillstyle(SOLID_FILL, DARKGRAY);
bar(0, 0, 640, 480);
// 2. Draw hatch house walls
setfillstyle(HATCH_FILL, CYAN);
bar(170, 200, 330, 380);
// 3. Draw roof using slash fill
setfillstyle(SLASH_FILL, RED);
fillpoly(4, points);
// 4. Draw door using solid fill
setfillstyle(SOLID_FILL, BROWN);
bar(230, 280, 270, 380);
getch();
closegraph();
return 0;
}
This code shows how you can combine different pattern and color configurations to create a basic textured shape composite. By changing the fill style before each call, each segment gets painted with a distinct texture.
Common Mistakes
Confusing argument order
The syntax requires (pattern, color). A common mistake is reversing the arguments to setfillstyle(color, pattern). Reversing these values will lead to invalid configuration codes or apply the wrong colors and textures (for example, setting the pattern to color index 14 and the color to pattern index 1).
Forgetting that USER_FILL requires setup
If you call setfillstyle(USER_FILL, RED) without first defining a custom pattern bitmap using setfillpattern(), BGI will execute undefined behavior, which often renders as a messy garbled pattern or crashes the graphics driver. Always set the custom pattern first before referencing USER_FILL.
Expecting outline colors to change
setfillstyle() determines how the *interior* pixels of shape boundaries are painted. It does not change the outline border drawing color, which is governed by setcolor(). For functions like floodfill(x, y, border_color), make sure you configure setcolor() for the boundary and setfillstyle() for the fill itself.
Try it online
Test patterns instantly. Copy-paste these codes into the browser-based editor panel to execute C BGI graphics programs directly in the browser.