import java.awt.*; import javax.swing.*; public class MyPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); int[] xPoints = {100, 150, 200}; int[] yPoints = {200, 100, 200}; g.setColor(Color.blue); g.fillPolygon(xPoints, yPoints, 3); } }
import java.awt.*; import javax.swing.*; public class MyPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); int[] xPoints = {100, 150, 200, 180, 120}; int[] yPoints = {200, 100, 200, 250, 250}; g.setColor(Color.yellow); g.fillPolygon(xPoints, yPoints, 5); } }This example creates a pentagon with 5 vertices and fills it with a yellow color using the fillPolygon method. The polygon has a more complex shape compared to the previous example. Package library: java.awt