Trying to draw a poly in Java...

Started by
1 comment, last by Zahlman 16 years, 9 months ago
Hey im doing my midterm for CS1400, and ran into a snag. First of all some background. We are supposed to use shapes to draw a picture. Our example was a bat man sign, which was created by using ovals. So I decided to try and create the transformer autobot sign, since it was just released, etc. I started to do ok. I used several Rectangles and Oval, and Arcs, but when I tried to use a polygon, the object doesn't show up on my screen. Here is my code for the 'Poly' class:

/**
 * Draws a Poly and fills it
 * 
 * @author Brett Unzaga
 * @version 07-14-07
 */

import javax.swing.*;
import java.awt.*;

public class Poly extends JComponent
{

    int[] XPoints;
    int[] YPoints;
    int NPoints;
    public Poly(int[] xPoints, int[] yPoints, int nPoints)
    {
        super();
        setBackground(Color.blue);
        NPoints=nPoints;
        XPoints = new int[NPoints];
        for (int i=0;i<NPoints;i++)
        {
            XPoints=xPoints;
        }
        YPoints=new int[NPoints];
        for (int i=0;i<NPoints;i++)
        {
            YPoints=yPoints;
        }
    }
    public void paint(Graphics g)
    {
        g.setColor(getBackground());
        g.fillPolygon(XPoints, YPoints, NPoints);
        paintChildren(g);
    }
}

And here is the code Im trying to call this method with. Please let me know if I have any redundant or un necessary code:

private Poly poly;

int[] X={200,100,100};
int[] Y={50,300,100};
poly = new Poly(X,Y,3);
poly.setBackground(Color.BLACK);
mainWin.add(poly,0);

I'd like to be able to just do something like: poly=new Poly([200,100,100],[50,300,100],3); but it tells me it is an 'Illegal start of expression' Thanks for any help!
Advertisement
Oh yes, one other thing.

I tried to draw a line, using (50,50,100,-25)

or (100,50,-100,25)

Basically the line just wouldn't show up...so I am guessing that you can't draw lines using negative numbers?

This doesn't very realistic to me, so how can I draw a line going from lower left to upper right?
Quote:Original post by BUnzaga
And here is the code Im trying to call this method with. Please let me know if I have any redundant or un necessary code:


Java arrays know their length, so it's a bit silly to have to pass an extra parameter for that. Just accept the two arrays: throw an InvalidArgumentException if they're different lengths (or if either is null), and use the .length of one of them when you make the drawing call. (The reason the API has that parameter is to give you flexibility to use only part of an existing array, but you don't need that here.)

Also, consider using System.arraycopy to copy arrays.

Quote:
I'd like to be able to just do something like:

poly=new Poly([200,100,100],[50,300,100],3);

but it tells me it is an 'Illegal start of expression'

Thanks for any help!


Yeah, I'd like to be able to too. And in Python, I can. ;)

Seriously, though, try:

// assuming the above suggested changespoly = new Poly(new int[] { 200, 100, 100 } , new int[] { 50, 300, 100 });


This topic is closed to new replies.

Advertisement