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



Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative