Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# problem drawing polygons.

liulca ruggiero
Greenhorn
Posts: 15
this is the problem I would like to draw four different polygons, I can not find the solution can you help me thanks in advance.

[Added code tags - see UseCodeTags for details]

m Korbel
Ranch Hand
Posts: 174
1. don't create nay a new Object insidepaintComponent, prepare all elements before,

2. inside paintComponent loop/iterate inside array and to change only coordinates for each of elements

liulca ruggiero
Greenhorn
Posts: 15
sorry I can not understand you can give me an example thanks in advance

Michael Dunn
Ranch Hand
Posts: 4632
your problem is that POLYGON contains all 25 points of the 4 polygons
when you create your 4 polygons, they all contain the same 25 points i.e. 4 identical polygons

you need to have this as a class variable (accessible by paintComponent())
Polygon[] polygons = new Polygon[4];

currently you have this
int n = POLYGON.size();
int[] x = new int[n];
int[] y = new int[n];

what you need to do is separate each iteration of POLYGON into its own polygon,
via POINTS.getID()-1 (the -1 is for a zero-based collection)
you will probably need to add an arraylist collection, as each polygons number of points differs,
then you create the polygons by iterating each of the arraylists.

if you do it correctly, this is *all* you need in paintComponent()

liulca ruggiero
Greenhorn
Posts: 15
the problem is that polygons are 4 different in different positions for example

pnt.getID (); polygon 1 .... id = 1 .... Total points = 3;
pnt.getID (); polygon 2 .... id = 2 .... Total points = 4;
pnt.getID (); polygon 3 .... id = 3 .... Total points = 6;
pnt.getID (); polygon 4 .... id = 4 .... Total points = 12;

I'm studying for a long time but I can not find the solution

Michael Dunn
Ranch Hand
Posts: 4632
> the problem is that polygons are 4
> different in different positions for
example>

that might be what you want, but all your
code does is create 4 identical polygons
each with 25 points.

liulca ruggiero
Greenhorn
Posts: 15
is in fact the code does not work but how can I modify it to draw polygons many thanks in advance.

Michael Dunn
Ranch Hand
Posts: 4632
> but how can I modify it to draw polygons

I told you the steps in my first reply.

if you still have problems, start at the beginning:
write a program to draw a triangle - that's all, nothing else.

when that works OK, modify the program to draw 2 triangles.

when that works, and you understand it, think about your
posted code, and how to separate all those POLYGON points
into their own polygon, like you have in your 2 triangles.

liulca ruggiero
Greenhorn
Posts: 15
my problem is that I can not distinguish one from another polygon based "id" if you can give me a help I thank you in advance.

Michael Dunn
Ranch Hand
Posts: 4632
> I can not distinguish one from another polygon based "id"

create an array[4] of arrayLists

iterate POLYGONS, for each POINTS (there are 25 of them), add it to array[pnt.getID()-1]

now you'll need a nested for loop, to iterate the array[] as the outer for loop,
the inner one iterates each individual arrayList - here your use of
int[] x, in[] y , n, counter is identical, but they are local to the outer loop,
so they are recreated each iteration of the outer loop.

after the inner loop finishes, and before the outer loop finishes,

liulca ruggiero
Greenhorn
Posts: 15
I'm sorry I tried to update the code but I can not how do I go to the shooting range the number of points give me an example thanks in advance.

int n = POLYGON.size();
int[] count_points = new int[n];
int[] x = new int[n];
int[] y = new int[n];
int counter = 0;

for (POINTS pnt : POLYGON) {
count_points[counter] = pnt.getID()-1;
x[counter] = pnt.getX();
y[counter++] = pnt.getY();
}

Polygon[] polygons = new Polygon[4];
for (int i = 0; i < polygons.length; i++) {
polygons[i] = new Polygon();
polygons[i] = new Polygon(x, y, count_points);
g.setColor(Color.RED);
g.drawPolygon(polygons[i]);
}}

Michael Dunn
Ranch Hand
Posts: 4632
just follow the steps in my previous post, tested it - works OK

liulca ruggiero
Greenhorn
Posts: 15
I'm sorry but I can not do it maybe because I'm not very experienced thank you in advance bye.

Michael Dunn
Ranch Hand
Posts: 4632
> I'm not very experienced

nothing to do with experience, all about understanding and learning, and you seem to have problems with both.

it has been explained to you numerous times that you are putting all 25 points into each polygon,
so let's have a look at part of the modified code from your latest attempt:

so this line
polygons[i] = new Polygon(x, y, count_points);
gives each polygon 25 x's and 25 y's

> bye.

agreed

liulca ruggiero
Greenhorn
Posts: 15
my problem is how to separate the polygons according to '"id" if your code works perfectly because you can not see the solution you have to study adopted thanks in advance.

Paul Clapham
Sheriff
Posts: 21107
32
liulca ruggiero wrote:my problem is how to separate the polygons...

That's because your original code threw everything into one single object called POLYGON, which actually contained data for all of the polygons. And that means that now you have to separate the data back out again.

So that means your data structure is the problem. Throw what you have away, and start again. Design a data structure in which a polygon is a list of points, and in which you have a set (e.g. array) of polygons.

Michael Dunn
Ranch Hand
Posts: 4632
this is from an earlier reply

create an array[4] of arrayLists

iterate POLYGONS, for each POINTS (there are 25 of them), add it to array[pnt.getID()-1]

you have 4 arrayLists in an array - arrayList[0],arrayList[1],arrayList[2],arrayList[3]
now iterate POLYGON
for (POINTS pnt : POLYGON)
and all you do is

now all your polygon points are in their own separate arraylist
and the rest is simple, really simple

liulca ruggiero
Greenhorn
Posts: 15
@ Michael Dunn I'm sorry I can not find the solution to the problem so I have to start all over again thanks in advance.

Michael Dunn
Ranch Hand
Posts: 4632
> so I have to start all over again

yes, that's the best thing to do.

run your original code, but comment out polygons 2,3 and 4,
so you'll only have the first one with 3 points.

if that works OK, try it again for polygon 2, commenting out 1,3 and 4
repeat for 3 and 4.