Results 1 to 1 of 1
  1. #1
    jtech55 is offline Member
    Join Date
    Mar 2011
    Rep Power

    Default 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()
    Random generator = new Random();
    setSize(1000, 500);
    width = getSize().width;
    height = getSize().height;
    //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 (;
    //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.

Similar Threads

  1. Polygons... returning and passing objects?
    By asherwolf in forum New To Java
    Replies: 3
    Last Post: 07-09-2010, 04:41 PM
  2. Create a Get/set for an array
    By mechdesignron in forum New To Java
    Replies: 4
    Last Post: 05-04-2010, 09:39 PM
  3. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 09:01 PM
  4. Replies: 0
    Last Post: 07-02-2008, 07:50 PM
  5. Demonstration of drawing polygons
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 06-28-2008, 09:24 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts