Wednesday, March 17, 2010

Creating Polygons

Other than the ones discussed, polygons  can be created through Java Graphics class.  A polygon can be made in two ways: polygon methods and Polygon class.

The polygon must take three arguments, find below the method syntax:

fillPolygon(xPoints_array,yPoints_array,array_length) - to draw a solid and filled shape through set of x and y points with number of points indicated. 

To make use of Polygon class with similar effect, we can use the constructor below:

Polygon newPolygon = new Polygon(xPoints,yPoints,size);


[+/-] show/hide

import java.applet.*;
import java.awt.*;

public class GraphicsRectsCircles extends Applet {
int xPoints[]={42,52,72,52,60,40,15,28,9,32,42};
int yPoints[]={38,62,68,80,105,85,102,75,58,60,38};
int size=xPoints.length;

public void paint(Graphics g)

//g.drawPolygon(xPoints,yPoints,xPoints.length); //to draw a polygon outline
g.fillPolygon(xPoints,yPoints,xPoints.length); //to fill a solid polygon
// or through the lines below, produces same result
Polygon myPolygon=new Polygon(xPoints,yPoints,size);

No comments: