Academic Java    Java Tutorial  >  Applets  >  fillPolygon()

fillPolygon() Example

Java fillPolygon EXAMPLE output This program draws the national flag of the Bahamas.

It uses two anonymous arrays, one for x-coordinates the other for y-coordinates, in the call to fillPolygon(), to create a black triangular-shaped polygon,
// fillPolygon Example
import java.awt.*;
import javax.swing.*;

class FillPolygonExample extends JApplet {

   @ Override public void paint(Graphics g) {
      super.paint(g);

      int w = 200, h = w/2, s2 = 150;

      g.setColor(Color.blue);
      g.fillRect(s2-w/2,s2-h/2,w,h);

      g.setColor(Color.yellow);
      g.fillRect(s2-w/2,s2-h/6,w,h/3);

      g.setColor(Color.black);
      g.translate(s2-w/2,s2);
      g.fillPolygon(new int[]{0,2*w/5,0},new int[]{h/2,0,-h/2},3);

   }
}


* drawPoloygon() fillPolygon() Examples