Polygon - Clique Drawing

Hello,

I wish to shorten my earlier posting for clarity. If I can re-implement the code below using a dynamic approach, then my problem in the earlier posting will be solved.

import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.Polygon;

import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

publicclass DrawCliqueTest{

publicstaticvoid main(String[] args){

try{

JFrame frame =new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

BufferedImage image =new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);

Graphics2D g = image.createGraphics();

g.setColor(Color.WHITE);

g.fillRect(0, 0, image.getWidth(), image.getHeight());

g.setColor(Color.BLACK);

Polygon poly =new Polygon(newint[]{150, 50, 250, 57},newint[]{50, 200, 203, 270}, 4);

// Draw a line between every pair of points

for (int i = 0; i < poly.npoints-1; i++)

for (int j = i+1; j < poly.npoints; j++)

g.drawLine(poly.xpoints[i], poly.ypoints[i], poly.xpoints[j], poly.ypoints[j]);

Polygon goy =new Polygon(newint[]{150, 278},newint[]{50, 80}, 2);

for (int i = 0; i < goy.npoints-1; i++)

for (int j = i+1; j < goy.npoints; j++)

g.drawLine(goy.xpoints[i], goy.ypoints[i], goy.xpoints[j], goy.ypoints[j]);

g.dispose();

frame.add(new JLabel(new ImageIcon(image)));

frame.pack();

frame.setVisible(true);

}catch (Exception e){e.printStackTrace();}

}

}

It actually works and draws two Polygons at the same time with one common intersection at x=150 and y=50 because I called;

Polygon poly =new Polygon(newint[]{150, 50, 250, 57},newint[]{50, 200, 203, 270}, 4);

and

Polygon goy =new Polygon(newint[]{150, 278},newint[]{50, 80}, 2);

seperately.

In the code above, I already know the number of polygons to be drawn is 2, so I defined and assigned their parameters to the Polygon method to draw the 2 objects.

Now, my problem is, in my program I will not know the number of the Polygons to be drawn because they will be dynamically generated, so, I also need to call Polygon poly = new Polygon(parameters, parameters, parameters) the same number of times to draw the different objects at the same time.

How can I rewrite the above program (and still get the same result) so that it can work dynamically? Any help?

Thanks,

Jona_T

Message was edited for clarity by:

Jona_T

[4870 byte] By [Jona_Ta] at [2007-11-27 4:48:54]
# 1

Place your polygons inside an array and loop over it...

Polygon[] polygons = ....;

// Draw a line between every pair of points

for (Polygon poly : polygons) {

for (int i = 0; i < poly.npoints-1; i++)

for (int j = i+1; j < poly.npoints; j++)

g.drawLine(poly.xpoints[i], poly.ypoints[i], poly.xpoints[j], poly.ypoints[j]);

}

Rodney_McKaya at 2007-7-12 10:01:53 > top of Java-index,Security,Cryptography...
# 2

Hello Rodney_McKay,

Please, I do not fully get you right. Can you give me an example on how to place polygons inside an array based on my first posting - Polygon goy = new Polygon(new int[] {150, 278}, new int[] {50, 80}, 2) and Polygon poly = new Polygon(new int[] {150, 50, 250, 57}, new int[] {50, 200, 203, 270}, 4);

Thanks a lot,

Jona_T

Jona_Ta at 2007-7-12 10:01:53 > top of Java-index,Security,Cryptography...
# 3

Polygon[] polygons = new Polygon[] {new Polygon(new int[] {150, 278}, new int[] {50, 80}, 2),

new Polygon(new int[] {150, 50, 250, 57}, new int[] {50, 200, 203, 270}, 4)};

Rodney_McKaya at 2007-7-12 10:01:53 > top of Java-index,Security,Cryptography...
# 4
Hello Rodney_McKay,Thanks, you get it right. I will try now to implement it in my bigger code and see what comes out. Your dukes are on the way.Jona_T
Jona_Ta at 2007-7-12 10:01:53 > top of Java-index,Security,Cryptography...
# 5

If you really want to keep thing dynamic you should use ArrayList instead of arrays.

This way you can add and remove polygons from your list.

ArrayList<Polygon> list = new ArrayList<Polygon>();

list.add(new Polygon(new int[] {150, 278}, new int[] {50, 80}, 2));

list.add(new Polygon(new int[] {150, 50, 250, 57}, new int[] {50, 200, 203, 270}, 4));

for (Polygon poly : list) {

for (int i = 0; i < poly.npoints-1; i++)

for (int j = i+1; j < poly.npoints; j++)

g.drawLine(poly.xpoints[i], poly.ypoints[i], poly.xpoints[j], poly.ypoints[j]);

}

Rodney_McKaya at 2007-7-12 10:01:53 > top of Java-index,Security,Cryptography...
# 6
Rodney_Mckay,Nice of you for the extra information. I will need an hour or more to rewrite my codes and will definitely give you a feed back.Jona_T
Jona_Ta at 2007-7-12 10:01:53 > top of Java-index,Security,Cryptography...
# 7

Hello Rodney_Mckay,

I told you earlier that it will take me an hour or more to rewrite my codes and give you a feedback. It later took me days to correct some terrible mistakes I made. Now it is working and I am gradually progressing. I wish to thank you once more for the help,

Jona_T

Jona_Ta at 2007-7-12 10:01:53 > top of Java-index,Security,Cryptography...