This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes problem drawing polygons. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem drawing polygons." Watch "problem drawing polygons." New topic
Author

problem drawing polygons.

liulca ruggiero
Greenhorn

Joined: Nov 21, 2012
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

Joined: Jun 19, 2012
Posts: 154
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

Joined: Nov 21, 2012
Posts: 15
sorry I can not understand you can give me an example thanks in advance
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
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

Joined: Nov 21, 2012
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
can be an example or your help thanks in advance.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
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

Joined: Nov 21, 2012
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

Joined: Jun 09, 2003
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

Joined: Nov 21, 2012
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

Joined: Jun 09, 2003
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,
your create your polygon from x,y and n
liulca ruggiero
Greenhorn

Joined: Nov 21, 2012
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

Joined: Jun 09, 2003
Posts: 4632
just follow the steps in my previous post, tested it - works OK
liulca ruggiero
Greenhorn

Joined: Nov 21, 2012
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

Joined: Jun 09, 2003
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

Joined: Nov 21, 2012
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jun 09, 2003
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
arrayList[pnt.getID()-1].add(pnt)

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

liulca ruggiero
Greenhorn

Joined: Nov 21, 2012
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

Joined: Jun 09, 2003
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.
 
 
subject: problem drawing polygons.
 
Similar Threads
complicated code, 900 labels.
Need help on animation using GUI Builder.
problem in threads(sun accesibility test)
Creating triangles using the Polygon class problem???
draw different polygons from a arraylist