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]

# 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);
}
# 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