Drawing Lines

Hello Java Friends,

I am relatively new to Java 2D and having some implementation problem.

I wanted to draw a shape e.g. (triangle or a figure with more nodes) and theneach node will be connected to the rest nodes with lines. E.g. I have a rectangle with four nodes A, B, C and D. A will connect to B, C and D. B will connect C, D and A. C will connect to D, A and B. D will connect A, B and C. It works only up to 3 nodes (triangle). Can someone give me some idea on how to solve the problem.

See code below:

//draw the line

GeneralPath circleLine =new GeneralPath(GeneralPath.WIND_EVEN_ODD, xPoints.length);

circleLine.moveTo(xPoints[0]+ circleSize[0]/2, yPoints[0] + circleSize[0]/2);

g2d.setColor(Color.green);

g2d.setStroke(new BasicStroke(1));

for (int index = 1; index < xPoints.length; index++){

for (int indey = 1; indey < yPoints.length; indey++){

circleLine.lineTo(xPoints[indey] + circleSize[0]/2, yPoints[indey] + circleSize[0]/2);

}

}

circleLine.closePath();

g2d.draw(circleLine);

Thanks,

Jona_T

[1444 byte] By [Jona_Ta] at [2007-11-26 23:58:51]
# 1

Here's an example how to draw a Clique:

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;

public class DrawCliqueTest {

public static void 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(new int[] {50, 250, 250, 50, 10}, new int[] {50, 50, 250, 250, 150}, 5);

// 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]);

g.dispose();

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

frame.pack();

frame.setVisible(true);

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

}

}

Rodney_McKaya at 2007-7-11 15:47:06 > top of Java-index,Security,Cryptography...
# 2
Hello Rodney_Mckay,That you for the great help. It really works.Jona_T
Jona_Ta at 2007-7-11 15:47:06 > top of Java-index,Security,Cryptography...
# 3
Hello Rodney_McKay,That is great. Thank you for the help.Jona_T
Jona_Ta at 2007-7-11 15:47:06 > top of Java-index,Security,Cryptography...