Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is fillpolygon() in java?

Shahzadi Jaweria

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The shot will discuss the fillPolygon() method in Java. It is preferred to call this method after calling drawPolygon() to get your drawn shape filled with the respective color. If we use this method without calling drawPolygon() before, it will not throw an exception. The program will compile and run successfully. However, it will not display anything as an output because we haven’t drawn the shape before filling it.

Introduction

fillpolygon() is a method of the Graphics class in Java. The Graphics class fills the shape of an already drawn polygon with color.

Default color

The default color is black, but we can change the color by using the Graphics class’ setColor(Color c) method.

Exception

It is compulsory to draw a polygon using the drawpolygon() method before using the fillpolygon()method. Otherwise, the program will not throw any exception, but the console’s output screen will show nothing.

Syntax

Graphics.drawPolygon(int []x, int []y, int noOfPoints)
Graphics.drawPolygon(Polygon p)

Explanation

int[]x is an integer array consisting of the required shape’s x-coordinates.

int[]y is an integer array consisting of the shape’s y-coordinates.

int numberOfPoints is an integer variable that declares the number of vertices the figure will have.

Polygon p is an object of Java’s Polygon built-in class.

import java.awt.*;
import javax.swing.*;

class Main extends JPanel {
    public void paintComponent(Graphics g) {
        int [] x = {45, 55, 75, 55, 63, 43, 17, 31, 12, 35, 45};
        int [] y = {41, 65, 71, 83, 108, 88, 105, 78, 61, 63,41};
        g.drawPolygon(x, y, 10);
        g.setColor(Color.BLUE);
        g.fillPolygon(x, y, 10);
        //Polygon p = new Polygon();
        //g.fillPolygon(p);
    }
    public static void main( String args[] ) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("Fill Polygon");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBackground(Color.white);
        frame.setSize(300, 200);

        Main panel = new Main();

        frame.add(panel);
        frame.setVisible(true);      
    }
}

Explanation

  • Line 1: We import Java’s AWT for making the graphic user interface (GUI).

  • Line 2: We import the swing API to make the GUI.

  • Line 4: We create a class and inherit it from JPanel.

  • Line 5: We override JPanel class’ paintComponent() method.

  • Line 6: We make an array of the points’ x-coordinates to draw a star (polygon).

  • Line 7: We make an array of the points’ y-coordinates to draw a star (polygon).

  • Line 8: We call the Graphic class’ built-in drawPolygon() method to draw a star (polygon). It takes three parameters: the array of x-coordinates, the array of y-coordinates, and the shape’s total number of points.

  • Line 9: We call the Graphic class’ built-in method, setColor(), to set the color that fills the shape. It takes one parameter, i.e., the color name which is an attribute of the Color class. If we don’t use it, the shape will be filled by the default color (black). This is optional.

  • Line 10: We call the Graphic class’ built-in fillPolygon() method to fill the star (polygon). It takes three parameters: the array of x-coordinates, the array of y-coordinates, and the shape’s total number of points.

  • Line 11: We create an object of the built-in Polygon class. This is an alternative option.

  • Line 12: We call the Graphic class’ built-in fillPolygon() method to fill the star (polygon). It takes only the object of the Polygon class as a parameter. This is an alternative option.

  • Line 14: We override the main() method.

  • Line 15: We let the system handle the decoration (i.e., the borders) of the window. The window that appears is decorated with fancy borders. This is optional.

  • Line 16: We create a JFrame object to create the window and name it “Fill Polygon.”

  • Line 17: The window closes on receiving a close window event.

  • Line 18(optional): We set the background color as white. This is optional.

  • Line 19: We set the window size.

  • Line 21: We create a Main class object that automatically calls the paintComponent() method (the one we have overridden)

  • Line 23: We add this component (i.e., the completely filled colored drawn polygon) into the window.

  • Line 24: We make the window visible by setting the flag true.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring