Fill and Patterns
Fill Styles & Patterns in graphics.h
In graphics.h, fills are state-based: you select a fill style and color, then every compatible shape uses those settings until you change them. This page explains setfillstyle(), setfillpattern(), and practical fill workflows.
On This Page
How Fill Settings Work
Fill behavior in graphics.h is global state. Once you call setfillstyle() or setfillpattern(), that fill setting stays active for future fill operations until another fill-setting call overrides it.
setfillstyle(SOLID_FILL, LIGHTGREEN); bar(60, 80, 220, 160); // uses LIGHTGREEN solid setfillstyle(HATCH_FILL, YELLOW); fillellipse(340, 120, 70, 45); // now uses HATCH_FILL + YELLOW
Because of this state model, it is safer to set fill style immediately before each filled shape.
setfillstyle()
void setfillstyle(int pattern, int color);
Sets built-in fill pattern and fill color for subsequent filled drawing operations.
setfillstyle(SOLID_FILL, LIGHTGREEN); bar(80, 80, 220, 180);
pattern: One of the predefined fill constants (SOLID_FILL,HATCH_FILL, etc.).color: Fill color from graphics.h color constants.
Common Fill Styles
SOLID_FILL: Fully solid region.LINE_FILL,LTSLASH_FILL,SLASH_FILL: line-based textures.HATCH_FILL,XHATCH_FILL: crosshatch effects.WIDE_DOT_FILL,CLOSE_DOT_FILL: dotted fills.USER_FILL: custom pattern viasetfillpattern().
For teaching and demos, SOLID_FILL gives best visibility. For contrast between regions, use HATCH_FILL or XHATCH_FILL.
setfillpattern()
void setfillpattern(char pattern[8], int color);
Defines a custom 8x8 tile pattern. Each byte represents one row in the tile, where each bit controls whether a pixel in that row is filled.
char bricks[8] = {0xFF,0x81,0xBD,0x81,0xFF,0x81,0xBD,0x81};
setfillpattern(bricks, LIGHTRED);
Use this when predefined styles are not enough and you need your own texture.
Where Fill Is Used
Fill settings affect functions that paint areas, such as:
bar(),bar3d(),fillellipse(),fillpoly()floodfill()(fills enclosed regions using current fill settings)
Outline-only functions like line(), rectangle(), and circle() do not use fill style by themselves.
Complete Runnable Example
This program compares three fill approaches: predefined solid fill, predefined hatch fill, and custom user pattern.
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
setcolor(WHITE);
// 1) Solid fill on ellipse
setfillstyle(SOLID_FILL, GREEN);
fillellipse(120, 120, 60, 40);
// 2) Hatch fill on bar
setfillstyle(HATCH_FILL, YELLOW);
bar(220, 70, 360, 180);
// 3) Custom user pattern + floodfill
char custom[8] = {0xAA,0x55,0xAA,0x55,0xAA,0x55,0xAA,0x55};
setfillpattern(custom, LIGHTMAGENTA);
rectangle(390, 70, 540, 180);
floodfill(400, 80, WHITE);
getch();
closegraph();
return 0;
}
Best Practices & Debugging
- Set fill style immediately before each filled shape to avoid accidental carry-over.
- For
floodfill(), ensure boundary is fully closed and border color argument is exact. - If a region fills with unexpected style, another fill call probably changed global fill state.
- Use simple patterns in beginner examples so shape boundaries remain readable.
If fill leaks outside a shape, first check for tiny gaps in borders and then verify the border color passed to floodfill().
Related: Colors & Palette and Polygons & Fill.