Open In App

Fill Area Function in Computer Graphics

Last Updated : 21 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Computer graphics is an important part of Computer science. This help to gain knowledge about the designs & the coloring. Computer graphics is the part that helps to create digital images with the help of coding. So, the images or the objects that are generated using the Computer graphics will have a trace of the programming. Computer graphics is the wing that provides the necessary elements to draw any object. Like there are lines, circles, rectangles, etc figures. Also, there are some coloring functions available. These are the functions that help to color a certain area. But, to use computer graphics, one should have the minimum imagination. So that, they can able to taste the essence of Computer graphics in a better way.

Fill Area Functions in Computer Graphics:

In computer graphics, there is a special function known as the fill function. This fill function is used to color a certain area. There are two components of the fill area function. These components are responsible to color a certain area. These components need to be placed in proper sequences. Otherwise, there will be an issue to color an area.

  1. Setfillstyle(int pattern, int color): This is the main component of the fill area function. This function has two arguments. One is the pattern argument. And another is the color argument. There are thirteen patterns available for this function. These all patterns can be used to create new designs in any certain area. And the color argument is responsible to color the designs. Any colors that are available in the Computer graphics can be used there.
  2. Floodfill(int x, int y, int border_color): This is another component of the fill area function. After declaring the area, this function needs to be used. There are two coordinates are present. These coordinates must be a coordinate inside of the figure. This means these coordinates will indicate the bounded area that needs to be colored. There is another argument, that is the border color. Using that argument, the border color can be changed promptly. But, most of the time the border color is White. The numerical representation of the color can be used there also.

Different Patterns Of The Setfillstyle() Function:

There are different patterns are available in the setfllstyle() function. These can be used along with a bounded figure. Also, after using the setfillstyle() function, we need to add the floodfill() functions. This will complete the total implementation process. Here, a rectangle is used for the bounded area. One by one all the patterns have been implemented.

In this case, in the setfillstyle() function, the pattern should be declared from the below-mentioned pattern list. Along with that, the programmer needs to specify the color of the style. Here, the GREEN color is used for demonstrating purposes. After using the setfillstyle() function, an area has been implemented. Here, the rectangle area is used. Then, the floodfill() function should be used. The programmer needs to provide the coordinates that are inside the area. So, the inner area will be colored. Programmers need to provide specific coordinates that are bounded. Otherwise, the external area will be colored.

EMPTY_FILL Pattern: This pattern can’t able to color anything. This is just like the null pattern. No color and no design will be there inside the area. So, this pattern is rarely used in computer graphics.

Example 1: 

C




#include<graphics.h>
#include<conio.h>
int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\turboc3\\bgi");
    // Implementing EMPTY_FILL Pattern
    setfillstyle(EMPTY_FILL,GREEN);
 
    // Rectangle Area
    rectangle(250,100,650,280);
 
    // Flood Fill Function
    floodfill(252,158,15);
 
    getch();
    closegraph();
    return 0;
}


Java




import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
 
public class Main extends JFrame {
 
    public Main()
    {
        setTitle("Rectangle with Empty Fill");
        setSize(900, 600);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
 
    public void paint(Graphics g)
    {
        super.paint(g);
        g.setColor(Color.GREEN);
        g.drawRect(250, 100, 400, 180);
    }
 
    public static void main(String[] args) { new Main(); }
}


Javascript




let gd = DETECT;
let gm;
initgraph(gd, gm, "C:\\turboc3\\bgi");
 
// Implementing EMPTY_FILL Pattern
setfillstyle(EMPTY_FILL,GREEN);
 
// Rectangle Area
rectangle(250,100,650,280);
 
// Flood Fill Function
floodfill(252,158,15);
 
getch();
closegraph();
 
// This code is contributed by ishankhandelwals.


Python3




#Python Code
from graphics import *
 
# Create Graph
gd = detect()
gm = GraphWin("C:\\turboc3\\bgi", 500, 500)
 
# Implementing EMPTY_FILL Pattern
gm.setFill(color_rgb(0,255,0))
 
# Rectangle Area
rect = Rectangle(Point(250,100), Point(650,280))
rect.draw(gm)
 
# Flood Fill Function
floodfill(gm, 252, 158, 15)
 
gm.getMouse()
gm.close()


Output:

 

SOLID_FILL Pattern: This pattern is highly used in computer graphics. Using this pattern, one can able to color the area. It will fully color the selected area with the help of the floodfill(). Here, the area will be completely colored Green.

Example 2:

C




#include<graphics.h>
#include<conio.h>
int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\turboc3\\bgi");
 
    // Implementing SOLID_FILL Pattern
    setfillstyle(SOLID_FILL,GREEN);
 
    // Rectangle Area
    rectangle(250,100,650,280);
 
    // Flood Fill Function
     floodfill(252,158,15);
 
    getch();
    closegraph();
    return 0;
}


Output:

 

LINE_FILL Pattern: In this pattern, a bunch of horizontal lines will be created inside the selected area. The lines will be auto-generated. This help to make some unique design for the area. Here also, the Green color has been used.

Example 3:

C




#include<graphics.h>
#include<conio.h>
int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\turboc3\\bgi");
 
    // Implementing LINE_FILL Pattern
    setfillstyle(LINE_FILL,GREEN);
 
    // Rectangle Area
    rectangle(250,100,650,280);
 
    // Flood Fill Function
    floodfill(252,158,15);
 
    getch();
    closegraph();
    return 0;
}


Output:

 

LTSLASH_FILL Pattern: This pattern is quite different. In this case, the area is a rectangle. The top-left & the bottom-right corner will be the starting area of this design. In this pattern, Slashes are being used to implement a design. As Green color is used here, the patterns will be also Green.

Example 4:

C




#include<graphics.h>
#include<conio.h>
int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\turboc3\\bgi");
 
    // Implementing LTSLASH_FILL Pattern
    setfillstyle(LTSLASH_FILL,GREEN);
 
    // Rectangle Area
    rectangle(250,100,650,280);
 
    // Flood Fill Function
    floodfill(252,158,15);
 
    getch();
    closegraph();
    return 0;
}


Output:

 

SLASH_FILL Pattern: This is the same as the LTSLASH_FILL pattern. The only difference is in this case, the brightness of the color gets increased than the LTSLASH_FILL pattern. So, programmers can use both patterns for the same reason.

Example 5:

C




#include<graphics.h>
#include<conio.h>
int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\turboc3\\bgi");
 
    // Implementing SLASH_FILL Pattern
    setfillstyle(SLASH_FILL,GREEN);
 
    // Rectangle Area
    rectangle(250,100,650,280);
 
    // Flood Fill Function
    floodfill(252,158,15);
 
    getch();
    closegraph();
    return 0;
}


Output:

 

BKSLASH_FILL Pattern: This is the opposite of the SLASH_FILL & LTSLASH_FILL pattern. In this case, the bottom-left & the top-right corner will be the starting point of this design. As the name suggests, this design is implemented with the help of the Back Slashes.

Example 6:

C




#include<graphics.h>
#include<conio.h>
int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\turboc3\\bgi");
 
    // Implementing BKSLASH_FILL Pattern
    setfillstyle(BKSLASH_FILL,GREEN);
 
    // Rectangle Area
    rectangle(250,100,650,280);
 
    // Flood Fill Function
    floodfill(252,158,15);
 
    getch();
    closegraph();
    return 0;
}


Output:

 

LTBKSLASH_FILL Pattern: This pattern is completely the same as the BKSLASH_FILL Pattern. But this pattern is quite a light color than the BKSLASH_FILL Pattern. As there is the word “LT” in front of the word, so this will create a light-colored version of the design. Programmers can use the LTBKSLASH_FILL Pattern & BKSLASH_FILL Pattern as per their choice.

Example 7:

C




#include<graphics.h>
#include<conio.h>
int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\turboc3\\bgi");
 
    // Implementing LTBKSLASH_FILL Pattern
    setfillstyle(LTBKSLASH_FILL,GREEN);
 
    // Rectangle Area
    rectangle(250,100,650,280);
 
    // Flood Fill Function
    floodfill(252,158,15);
 
    getch();
    closegraph();
    return 0;
}


Output:

 

HATCH_FILL Pattern: This is quite a different pattern. There will be a small box-like structure in the selected area. There will be a lot of box-like designs in the rectangle. This is another type of design.

Example 8:

C




#include<graphics.h>
#include<conio.h>
int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\turboc3\\bgi");
 
    // Implementing HATCH_FILL Pattern
    setfillstyle(HATCH_FILL,GREEN);
 
    // Rectangle Area
    rectangle(250,100,650,280);
 
    // Flood Fill Function
    floodfill(252,158,15);
 
    getch();
    closegraph();
    return 0;
}


Output:

 

XHATCH_FILL Pattern: This is quite similar to the HATCH_FILL Pattern. In the HATCH_FILL Pattern, the box-like design comes out. But in this case, the net-like structure will come out. This design is made with Rombos-like figures.

Example 9:

C




#include<graphics.h>
#include<conio.h>
int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\turboc3\\bgi");
 
    // Implementing XHATCH_FILL Pattern
    setfillstyle(XHATCH_FILL,GREEN);
 
    // Rectangle Area
    rectangle(250,100,650,280);
 
    // Flood Fill Function
    floodfill(252,158,15);
 
    getch();
    closegraph();
    return 0;
}


Output:

 

INTERLEAVE_FILL Pattern: This is another type of pattern. This might seems similar to the SOLID_FILL Pattern, but they are not the same. There are small boxes are placed one after another. So, there is no gap between them. So, it might seem there is a solid color present.

Example 10:

C




#include<graphics.h>
#include<conio.h>
int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\turboc3\\bgi");
    // Implementing INTERLEAVE_FILL Pattern
    setfillstyle(INTERLEAVE_FILL,GREEN);
  
    // Rectangle Area
    rectangle(250,100,650,280);
  
    // Flood Fill Function
    floodfill(252,158,15);
  
    getch();
    closegraph();
    return 0;
}


Output:

 

WIDE_DOT_FILL Pattern: As the name suggests, there are a lot of dots placed with maintaining some distance between them. So, the dots are placed in a wide range. In this way, a particular pattern is generated.

Example 11:

C




#include<graphics.h>
#include<conio.h>
int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\turboc3\\bgi");
 
    // Implementing WIDE_DOT_FILL Pattern
    setfillstyle(WIDE_DOT_FILL,GREEN);
 
    // Rectangle Area
    rectangle(250,100,650,280);
 
    // Flood Fill Function
    floodfill(252,158,15);
 
    getch();
    closegraph();
    return 0;
}


Output:

 

CLOSE_DOT_FILL Pattern: This is the opposite of the WIDE_DOT_FILL Pattern. Here, the dots are placed very near to each other. So, there is hardly any space between them. So, a large number of dots are collectively completed in this design.

Example 12:

C




#include<graphics.h>
#include<conio.h>
int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\turboc3\\bgi");
 
    // Implementing CLOSE_DOT_FILL Pattern
    setfillstyle(CLOSE_DOT_FILL,GREEN);
 
    // Rectangle Area
    rectangle(250,100,650,280);
 
    // Flood Fill Function
    floodfill(252,158,15);
 
    getch();
    closegraph();
    return 0;
}


Output:

 

USER_FILL Pattern: This is a very rarely used pattern. The output of this pattern & the SOLID_FILL Pattern will be the same. This will also create a solid color structure. So, programmers use it very less compared to SOLID_FILL Pattern.

Example 13:

C




#include<graphics.h>
#include<conio.h>
int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\turboc3\\bgi");
 
    // Implementing USER_FILL Pattern
    setfillstyle(USER_FILL,GREEN);
 
    // Rectangle Area
    rectangle(250,100,650,280);
 
    // Flood Fill Function
    floodfill(252,158,15);
 
    getch();
    closegraph();
    return 0;
}


Output:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads