Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# draw a triangle

Iain Palmer
Ranch Hand
Posts: 56
i am writing a program that ask you to think of a shape then ask how many sides it has got, then I would like to draw the shape be it a circle, traingle, square/rectangle. I can draw a circle and square/rectangle, but am having difficulty draw a traingle. I am using the polygon class in the awt package
here is a sample bit of a test program.

When I compile it I get the followig error
C:\Documents and Settings\Iain Palmer\Java2dGraphics\src\java2dgraphics\TriangleTest.java:23:
illegal start of expression
private Shape = new Polygon(int [0,50,0] xPoints,int[0,50,0] yPoints);
1 error

Jeff Albertson
Ranch Hand
Posts: 1780
Let me suggest that your syntax error isn't about triangles or polygons per se, but about how to create an array, initialize it and pass it as an argument to a constructor or method. Here are three ways to do that.

In the first version, I create, intialize and passing the arrays in separate stages. This is death by tedium here, but necessary if the intializiation is more complex, say if it involved a loop:

In the second version I use the special syntax of an array initializer to provide initial values.

In the third and tersest example, I create, initialize and pass the arrays all together. If you are uncomfortable with this shortest version, stick to the second one.

Now in your code, you wrote there are several mistakes:

1. when you invoke a method or constructor, don't repeat the argument type (int) or the arguments
formal parameter name (xPoints, yPoints). I also think you confused array notation -- [] -- with the array initializer -- {0, 50, 0}.

Iain Palmer
Ranch Hand
Posts: 56
Brillliant thank you it was just the problem with the initialization