Assignment #3: Regular Polygons


Score: 10 points + 1 bonus point
Due: Friday, Sept. 22, 2006

This assignment is about GUIs and custom drawing. You will have to draw a picture using the java.awt.Graphics 'fillPolygon()' method.

Your task:
Write a program, that draws a regular polygon like this:

The buttons to the left and right have the functionality to decrease or increase the number of vertices of the polygon (in the example above pressing the left button would result in a square, pressing the right button would replace the pentagon by a hexagon). The slider at the bottom rotates the polygon.
I prepared a method for you that returns the vertices of such a polygon, so you don't have to do any math (feel free to understand the method, though). You have to program the GUI, using appropriate Layouts and Containers, and you have to do the actual drawing of the polygon, using fillPoly(). You also have to program the functionality of increasing and decreasing the number of vertices in reaction to the button. You get 1 bonus point for implementing the slider (see below).

The task in steps:


You get the implementation of the method 'getRegularPolygon':
public int[][] getRegularPolygon(int numberOfVertices, int rotationAngle, int size)
The function returns a 2 dimensional array V of vertex coordinates, V[.][0]=x, V[.][1]=y. You pass the number of vertices you want to have, the rotation (in degrees, e.g. between -180 and 180), and the size of your panel (in the example above: 500). The vertex coordinates are all between 0 and the given size.

How to use the method:
Example: you want to draw a regular polygon with 8 vertices (an octagon), rotated by 45 degrees on a panel that has a size of 500x500:
int vertices[ ][ ]=getRegularPolygon(8,45,500);
Since you want to draw this polygon with fillPolygon(int x[ ], int y[ ], int n), you have to change the 2 dimensional array into 2 one dimensional arrays before you can actually use them. But at least all the math is done...


That's it. Enjoy !



The code:

method getRegularPolygon()