Academic Java    Java Tutorial  >  Applets  >  drawPoloygon() fillPolygon()

drawPoloygon() fillPolygon() Example

Java drawPoloygon() fillPolygon EXAMPLE output drawPolygon() and fillPolygon() draw a closed polygon defined by arrays of x and y int coordinates. Each pair of (x, y) coordinates defines a point. The third parameter of the calls is the number of points.

This example calls fillPolygon() to create the red L-shape, then it calls drawPolygon() to draw a black polygon around the L-shape using the same arrays of coordinates. Finally the example calls drawPolygon() to draw a black square-shape polygon as a border.
// drawPolygon(), fillPolygon() Example
import java.awt.*;
import javax.swing.*;

class DrawPolygonFillPolygonExample extends JApplet {

   @Override public void paint(Graphics g) {

      super.paint(g);

      g.setColor(Color.red);

      int xa[] = {125,125,150,150,200,200};
      int ya[] = {200,100,100,175,175,200};

      g.fillPolygon(xa, ya, 6);

      g.setColor(Color.black);
      g.drawPolygon(xa, ya, 6);

      int xa2[] = {100,100,225,225};
      int ya2[] = {225,75,75,225};

      g.drawPolygon(xa2, ya2, 4);

   }
}


* drawPoloygon() fillPolygon() Examples