import java.awt.*; public class PolygonExample extends Canvas { public void paint(Graphics graphics) { // create an array of x coordinates int[] xPoints = {100, 150, 200, 150}; // create an array of y coordinates int[] yPoints = {100, 150, 100, 50}; // define the number of points in the polygon int numPoints = 4; // draw a polygon with the specified coordinates graphics.drawPolygon(xPoints, yPoints, numPoints); } }
import java.awt.*; public class TriangleExample extends Canvas { public void paint(Graphics graphics) { // create an array of x coordinates int[] xPoints = {100, 150, 200}; // create an array of y coordinates int[] yPoints = {200, 150, 200}; // define the number of points in the polygon int numPoints = 3; // fill the polygon with the specified coordinates graphics.fillPolygon(xPoints, yPoints, numPoints); } }In this example, an array of x and y coordinates is created, representing the corners of a triangle. The fillPolygon method is then called to fill in the triangle with a color. Overall, the drawPolygon method is a powerful tool for creating custom shapes and graphics in Java applications.