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

