Tuesday, 29 November 2016

Program to draw Polygons on Applet

This program demonstrates how to draw and fill polygons. The drawPolygon( ) method draws a closed polygon defined by arrays of x and y coordinates. Each pair of (x, y) coordinates defines a point. The drawPolygon method's third parameter, nPoints, is the number of points in the polygon and should equal the number of pairs in the xPoints and yPoints arrays. The syntax for drawPolygon( ) is given below:
void drawPolygon (int[ ] xPoints, int[ ] yPoints, int nPoints)

The fillPolygon( ) method Fills a closed polygon defined by arrays of x and y coordinates. The syntax for fillPolygon( ) is given below:
void fillPolygon (int[ ] xPoints, int[ ] yPoints, int nPoints)

PROGRAM
// Draw and fill polygons
import java.awt.Graphics;

public class DrawFillPolygon extends java.applet.Applet
{
 int xCoords[] = { 50, 200, 300, 150, 50, 50 };
 int yCoords[] = { 100, 0, 50, 300, 200, 100 };
 int xFillCoords[] = { 450, 600, 700, 550, 450, 450 };
 public void paint(Graphics g)
 {
  g.drawPolygon(xCoords, yCoords, 6);
  g.fillPolygon(xFillCoords, yCoords, 6);
 }
}
/* <applet code=DrawFillPolygon width=300 height=300>
  </applet>
*/

OUTPUT

C:\>javac DrawFillPolygon.java
C:\>appletviewer DrawFillPolygon.java



No comments:

Post a Comment