# Thread: Create an array of polygons

1. Member
Join Date
Mar 2011
Posts
1
Rep Power
0

## Create an array of polygons

The program below I made draws a field with trees randomly placed.
The trees are triangles drawn by lines, and the stumps are filled rectangles drawn. I want to make the triangles drawn by the lines, filled too. I think the best way is fillPolygon but i cant create an array of polygons. Can someone help me fill my trees?

import java.applet.*;
import java.awt.*;
import java.awt.Polygon;
import java.awt.event.*;
import java.util.*;

public class Forrest extends Applet{
int width, height,length,numba,x,a;
int num=1000;

//top left x,y point of triangle
int tlx[][] = new int[num][num];int tly[][] = new int[num][num];
//bottom left x,y point
int blx[][] = new int[num][num];int bly[][] = new int[num][num];
//bottom right x,y point
int brx[][] = new int[num][num];int bry[][] = new int[num][num];

public void init()
{
setBackground(Color.GREEN);
Random generator = new Random();
setSize(1000, 500);
width = getSize().width;
height = getSize().height;
a=0;
x=0;
//side length of triangles
length = 25;

for (x=0; x <= num-1; x=x+1)
{
//generates triangle tip x,y point
tlx[x][a] = generator.nextInt((width)-length) + 10;
tly[x][a] = generator.nextInt((height)-length) + 10;
//creates other triangle points
blx[x][a] = tlx[x][a]-(length/2);
bly[x][a] = tly[x][a]+length;
brx[x][a] = tlx[x][a]+(length/2);
bry[x][a] = tly[x][a]+length;

}

}

public void paint (Graphics g )
{

for (x=0; x <= num-1; x=x+1 )
{
g.setColor (Color.black);
//draw triangles
g.drawLine( tlx[x][a],tly[x][a], blx[x][a],bly[x][a]);
g.drawLine( blx[x][a],bly[x][a], brx[x][a],bry[x][a]);
g.drawLine( brx[x][a],bry[x][a], tlx[x][a],tly[x][a]);

//draw stump
g.setColor(new Color(139, 69, 19));
g.fillRect(blx[x][a]+(length/4)+(length/8), bly[x][a], length/4, length/4);

}

}}
Last edited by jtech55; 03-18-2011 at 08:51 PM.