Polygon/Triangle Example

 

The three points of the triangle were (50,30), (70,70) and (30,70).

import java.applet.Applet;

import java.awt.*;

public class triangle extends Applet

{

    public void paint (Graphics g)

    {

        g.setColor (Color.red);

        //Put the points in an array

        int XArray [] = {50, 70, 30, 50};

        int YArray [] = {30, 70, 70, 30};

        //Draw the triangle

        g.fillPolygon (XArray, YArray, 4);

        //Draw the blue border

        g.setColor (Color.blue);

        g.drawPolygon (XArray, YArray, 4);

    }

}

 

A more complex example:

 

import    java.applet.Applet;
   import java.awt.*;
/* Shows how Polygon works
   * Draws a star on the screen with a black border
   * Choose a height and width of 500, 500 for your applet
   */
public class star extends Applet
   {
 public void paint(Graphics g)
   {
   g.setColor(Color.yellow);
   //Put the points in an array
   int XArray[]={60, 180, 220, 260, 380, 300, 320, 220, 120, 160, 60};
   int YArray[]={160, 160, 40, 160, 160, 240, 380, 280, 380, 240, 160};
   //Draw the star
   g.fillPolygon(XArray, YArray, 11);
 //Draw the black border
   g.setColor(Color.black);
   g.drawPolygon(XArray, YArray, 11);
   }
   
   }