Drawing triagles and other shapes

Hello Friends,

Is there a method or means in java that I can use to draw a triangle and other shapes using random generated integers without the Image looking funny.

In my case, because the numbers are randomly generated, it produces at times some funny Images.

for (int p = 0; p < size; p++){

xPoints[p] = 50 * 2 + randomNumber.nextInt(400);

yPoints[p] = 25 * 3 + randomNumber.nextInt(400);

}

Thanks,

Jona_T

[585 byte] By [Jona_Ta] at [2007-11-27 1:37:48]
# 1
You may consider having a check on area of the figure. given that any shape (here, i assume polygons) can be broken into triangles, it must be easy to implement too.
kotaonlinea at 2007-7-12 0:48:54 > top of Java-index,Security,Cryptography...
# 2
Hello Kotaonline,Thanks for your idea. I will implement it today and give you a feedback.Best regards,Jona_T
Jona_Ta at 2007-7-12 0:48:55 > top of Java-index,Security,Cryptography...
# 3

Here's a method you can use to make symmetrical polygons.

You can use random values in the expression for R which determines the size and for

the center point coordinates (cx, cy).

private int[][] generateShapeArrays(int cx, int cy, int R, int sides) {

int radInc = 0;

if(sides % 2 == 0)

radInc = 1;

int[] x = new int[sides];

int[] y = new int[sides];

for(int i = 0; i < sides; i++) {

x[i] = cx + (int)(R * Math.sin(radInc*Math.PI/sides));

y[i] = cy - (int)(R * Math.cos(radInc*Math.PI/sides));

radInc += 2;

}

// keep base of triangle level

if(sides == 3)

y[2] = y[1];

return new int[][] { x, y };

}

One way to use this method:

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

int w = getWidth();

int h = getHeight();

int R = Math.min(w,h)/8;

int[][] xy = generateShapeArrays(w/4, h/4, R, 6);

Polygon p = new Polygon(xy[0], xy[1], 6);

g2.draw(p);

}

crwooda at 2007-7-12 0:48:55 > top of Java-index,Security,Cryptography...
# 4
You can use the library http://java-sl.com/shapes.htmlto draw Regular polygons, stars, and polygons with rounded angles.regards,Stas
StanislavLa at 2007-7-12 0:48:55 > top of Java-index,Security,Cryptography...
# 5

Hello,

I partially solved the problem by using an iterator and Math.abs() to find the difference of two points. If the difference is less than or equal to a given threshold, say 75, then one of the points will be increased by 100 or any other figure which is higher than 75. The performance is good but not optimal. So, I will surely try your method to see which one performs better.

Thanks for the help.

Jona_T

Jona_Ta at 2007-7-12 0:48:55 > top of Java-index,Security,Cryptography...