# How to use Polygon class?

• 04-24-2010, 07:04 PM
lost_soul
How to use Polygon class?
Could someone show me how to use a Polygon class to draw shapes or try to walk me through it. How can I set it up to draw these coordinates and fill them in?

shape1 with:
xcoordinates: {139, 170, 203, 203}
ycoordinates: {139, 122, 122, 139}

shape2 with:
xcoordinates: {210, 245, 245, 210}
ycoordinates: {122, 122, 139, 139}

Code:

```import javax.swing.JPanel; import java.awt.*; import java.lang.Object; public class Shapes extends JPanel {   Polygon () Polygon (int [] xpoints, int[] ypoints, int npoints) Polygon shape = new Polygon shape.addPoint(int[] xpoints, int[] ypoints); Polygon shape2 = new Polygon shape2.addPoint(int[] xpoints, int[] ypoints); }```
• 04-24-2010, 08:05 PM
m00nchile
Seriously, google is your friend when you encounter a class that you don't fully understand. Either find the API, or google an example. To make your job a bit easier, here's the API for Polygon class Polygon API.
• 04-24-2010, 09:41 PM
lost_soul
Ok, I tried to set it up like this.
What are these errors telling me?

Code:

```import javax.swing.JPanel; import java.awt.*; public class shape extends JPanel   {   int[]xShape = new int[4];   int[]yShape = new int[4];   int nShape;  // count of points     // Make a shape   xShape[0]=139; xShape[1]=170; xShape[2]=203; xShape[3]=203;   yShape[0]=139; yShape[1]=122; yShape[2]=122; yShape[3]=139;   nShape = 4;     Polygon Shape = new Polygon(xShape, yShape, nShape);  // a shape   }```
I get these errors:

hape.java:19: ']' expected
xShape[0]=139; xShape[1]=170; xShape[2]=203; xShape[3]=203;
^
Shape.java:19: ']' expected
xShape[0]=139; xShape[1]=170; xShape[2]=203; xShape[3]=203;
^
Shape.java:19: ']' expected
xShape[0]=139; xShape[1]=170; xShape[2]=203; xShape[3]=203;
^
Shape.java:19: ']' expected
xShape[0]=139; xShape[1]=170; xShape[2]=203; xShape[3]=203;
^
Shape.java:20: ']' expected
yShape[0]=80; yShape[1]=80; yShape[2]=130; yShape[3]=170;
^
Shape.java:20: ']' expected
yShape[0]=80; yShape[1]=80; yShape[2]=130; yShape[3]=170;
^
Shape.java:20: ']' expected
yShape[0]=80; yShape[1]=80; yShape[2]=130; yShape[3]=170;
^
Shape.java:20: ']' expected
yShape[0]=80; yShape[1]=80; yShape[2]=130; yShape[3]=170;
^
shape.java:21: <identifier> expected
nShape = 18;
^
• 04-24-2010, 09:48 PM
Fubarable
I don't have a compiler present, but it looks like you're trying to make a whole bunch of method calls outside of a method/constructor/etc... You can't just place these calls out naked in the class.

i.e, This is no good:

Code:

```public class Foo {     System.out.println("hello, my name is Fubarable"); }```

But this is just fine:
Code:

```public class Foo {   public Foo() {     System.out.println("hello, my name is Fubarable");   }   public static void main(String[] args) {     Foo myFoo = new Foo();   } }```
• 04-25-2010, 12:08 AM
lost_soul
Ok, thank you.
I've tried it like this now.

These are the errors I'm getting. The example didn't use a return type so I'm not sure what it means by return type in the error?

Shape.java:13: invalid method declaration; return type required
public Polygon()
^
Shape.java:27: class, interface, or enum expected
}
^
2 errors

Code:

```import javax.swing.JPanel; import java.awt.*; public class Shape extends JPanel   {       public Polygon()     {     int[]xShape = new int[4];     int[]yShape = new int[4];     int nShape;  // count of points       // Make a shape     xShape[0]=139; xShape[1]=170; xShape[2]=203; xShape[3]=203;     yShape[0]=139; yShape[1]=122; yShape[2]=122; yShape[3]=139;     nShape = 4;     }     public static void main(String[] args)     {     Polygon myPolygon = new Polygon(xShape, yShape, nShape);     }   }```
• 04-25-2010, 12:13 AM
Fubarable
Now you're confusing me and the compiler. Is your class named Polygon (as it seems to be for the constructor and when you use it in main) or Shape (as you have declared it in the class signature)?

My suggestion: don't use either term since both names are part of the java Graphics libraries, but also make sure that the name you use for the class is the same as the file name as the constructor name and as the name you use in the main method. And keep studying the basics.
• 04-25-2010, 01:12 AM
lost_soul
Sorry, I'm confused also. I thought Polygon was part of the java.awt library so I'm trying to figure out how to call it rather then defining a method with it's own name.

I changed the class name to match the method name. If that's correct way to go.

These are the errors it's showing me now:

Shape.java:21: cannot find symbol
symbol : variable xShape
location: class Shape
Polygon myShape = new Shape(xShape, yShape, nShape);
^
Shape.java:21: cannot find symbol
symbol : variable yShape
location: class Shape
Polygon myShape = new Shape(xShape, yShape, nShape);
^
Shape.java:21: cannot find symbol
symbol : variable nShape
location: class Shape
Polygon myShape = new Shape(xShape, yShape, nShape);
^
Shape.java:21: internal error; cannot instantiate Shape.<init> at Shape to ()
Polygon myShape = new Shape(xShape, yShape, nShape);
^
Shape.java:28: cannot find symbol
symbol : variable myShape
location: class Shape
g.fillPolygon(myShape); // fills shape above.
^
5 errors

Code:

```import javax.swing.JPanel; import java.awt.*; public class Shape extends JPanel {     public Shape()         {             int[]xShape = new int[4];                 int[]yShape = new int[4];                 int nShape;  // count of points                                                 // Make a shape                 xShape[0]=139; xShape[1]=170; xShape[2]=203; xShape[3]=203;                 yShape[0]=139; yShape[1]=122; yShape[2]=122; yShape[3]=139;                 nShape = 4;         }                                public static void main(String[] args)         {             Polygon myShape = new Shape(xShape, yShape, nShape);         }                 public void paintComponent(Graphics g)         {       super.paintComponent(g);    // paint background       g.fillPolygon(myShape);    // fills shape above.     }   }```
• 04-25-2010, 01:28 AM
Fubarable
You've got some very pernicious errors that have nothing to do with Swing coding and everything to do with understanding the basics of Java. For example:

Code:

`    Polygon myShape = new Shape(xShape, yShape, nShape);`
So you appear to be creating a Polypgon variable, myShape and trying to construct it with a Shape constructor. So again, what is this? A Polypgon or a Shape? You have to make up your mind and choose one or the other. If it's a Shape object, you're calling a constructor that is not present in the class.

Here:
Code:

```  public static void main(String[] args) {     Polygon myShape = new Shape(xShape, yShape, nShape);   }   public void paintComponent(Graphics g) {     super.paintComponent(g); // paint background     g.fillPolygon(myShape); // fills shape above.   } }```
You appear to be trying to use a variable, myShape that is declared and used in the main method, a static method, in the class itself, something that's impossible to do and not desired even if it were possible. The myShape variable exists only in the main method and is only visible in the main method.

You may benefit from tutoring to overcome some misunderstandings of the basics of Java. I strongly urge you to get together with your instructor or tutor and soon.

Best of luck!
• 04-25-2010, 01:48 AM
Fubarable
If I were going to do this I'd create a class called UsePolygon that extends JPanel. As I mentioned previously, I'd not use the name Shape since that's already a class in the Java libraries.

I'd give the class two Polygon objects, polygon1 and polygon2 that are initialized with the arrays that you have posted in your OP and that used the Polygon constructor that I've found in the API: Polygon (Java Platform SE 6)

I'd have this class override paintComponent, as you are doing, and in this method, I'd create a Graphics2D object that is assigned the Graphics object that is passed into the paintComponent method's Graphics parameter. You'll have to cast the object into a Graphics2D object. I'd then have this method draw the two polygons using the Graphics2D draw method.

I'd then display this JPanel in a JFrame.

Again, best of luck.