# draw 4 different polygons.

Show 40 post(s) from this thread on one page
Page 2 of 2 First 12
• December 11th, 2012, 11:54 AM
liulca
Re: draw 4 different polygons.
hello
how do I save id, x, y array polygons

Code java:

```< Polygon[] polygons = new Polygon[4]; int id_number = 0; int x = 0; int y = 0; for (POINTS n : POLYGON) { id_number = n.getID(); x = n.getX(); y = n.getY(); id_number++; x++; y++; } >```
• December 11th, 2012, 12:10 PM
Norm
Re: draw 4 different polygons.
Outside the loop: Define two arrays for the x and y values and an index to use with them.
Inside the loop: assign the current POINT's x and y values in the arrays and increment the index:
x[index] = POINT's x value
y[index] = POINT's y value
index++
• December 11th, 2012, 03:51 PM
liulca
Re: draw 4 different polygons.
hello
I apologize as we move forward I can not see me for help

Code java:

```< int[] x = null ; int[] y = null ; int index = 0;   for (POINTS pnt : POLYGON) { x[index] = pnt.getX(); y[index++] = pnt.getY(); } >```
• December 11th, 2012, 04:09 PM
Norm
Re: draw 4 different polygons.
The x an y arrays should be defined the same size as the POLYGON collection.This is copied from your code:
Code :

```int n = POLYGON.size() ;   int[] x = new int[n]; int[] y = new int[n];```

Now look at the steps in post #15.
You need to recognize when the id for the POINTs object changes.
When it changes then you are at the end of the points for the last id and need to create a Polygon object that has all the points that have been saved in the x and y arrays and save that new Polygon object in the polygons array.
• December 11th, 2012, 04:28 PM
liulca
Re: draw 4 different polygons.
hello
I apologize to recognize when the id changes should I use if () {}
or how do I know when the id changes and pass the points array
and draw thank you in advance
Code java:

```<   int n = POLYGON.size() ; int[] x = new int[n]; int[] y = new int[n]; int index = 0;     Polygon[] polygons = new Polygon[4]; for (POINTS pnt : POLYGON) { x[index] = pnt.getX(); y[index++] = pnt.getY();   } >```
• December 11th, 2012, 04:32 PM
Norm
Re: draw 4 different polygons.
Quote:

how do I know when the id changes
When the value returned by the getID() method changes.
• December 11th, 2012, 05:15 PM
liulca
Re: draw 4 different polygons.
hello
I'm sorry I can not go on I thank you in advance.:confused:

Code java:

```<   int n = POLYGON.size() ; int[] x = new int[n]; int[] y = new int[n]; int index = 0;     Polygon[] polygons = new Polygon[4]; for (POINTS pnt : POLYGON) { int id = pnt.getID(); x[index] = pnt.getX(); y[index++] = pnt.getY(); } >```
• December 11th, 2012, 05:38 PM
Norm
Re: draw 4 different polygons.
You need an int variable that keeps track of the id of the POINTS. It should start at 1 to match the id of the first POINTS in the POLYGON collection.
• December 12th, 2012, 10:43 AM
liulca
Re: draw 4 different polygons.
hello
I'm sorry but how do I save the collection :confused:

Code java:

```< int n = POLYGON.size() ; int[] x = new int[n]; int[] y = new int[n]; int index = 0; int id = 0;   Polygon[] polygons = new Polygon[4]; for (POINTS pnt : POLYGON) { id = pnt.getID()-1; x[index] = pnt.getX(); y[index++] = pnt.getY(); id++; } >```
• December 12th, 2012, 10:48 AM
Norm
Re: draw 4 different polygons.
Quote:

how do I save the collection
What variable contains a reference to the collection?
What do you mean by "save"? Write it to disk file or put it into a database?
• December 12th, 2012, 10:57 AM
liulca
Re: draw 4 different polygons.
hello
I'm sorry not to save but to pass the points based
on the id to polygons and then draw that is the question
• December 12th, 2012, 11:11 AM
Norm
Re: draw 4 different polygons.
If it is too complicated now to separate the x,y pairs into separate Polygons based on their id, put all of the x,y points into one Polygon object. Change the loop in the paintComponent() method that loops through the array of Polygon obects to only draw the first polygon (which will have the points for all 4 polygons).
When that is working, then come back to the logic that will detect when the next POINTS from the POLYGON collection has a different id from the last POINTS object and use that to create a Polygon object for the x,y points saved so far and to reset the pointer to start saving the x,y points for the next id.
Show 40 post(s) from this thread on one page
Page 2 of 2 First 12