# Triangles wont draw Help?

• 03-22-2010, 12:37 AM
Triangles wont draw Help?
I have this simple applet using Graphics2D. It complies but the triangles will not draw. I know they are getting the numbers from the array but it does not seem to be drawing.

Code:

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;

public class ocotogonarray extends JApplet
{
public  void paint(Graphics g)
{
int i, j = 0;

int[] pointarray = { 36, 36, 0, 108, 36, 180,
36, 36, 0, 108, 36, 180, 180, 180, 216, 108, 180, 36};
// points for the triangles

super.paint(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.green);
int x, y;
BasicStroke b;
b = new BasicStroke(2, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND);
g2.setStroke(b);

Rectangle2D.Float subset = new Rectangle2D.Float(36, 36, 144, 144);
//stand in for the Jframe that will eventually be there
g2.draw(subset);

for(i=0; i<3; i++)
{
GeneralPath triangle;
triangle = new GeneralPath();
triangle.moveTo(pointarray[j], pointarray[j++]);
j++;
triangle.moveTo(pointarray[j], pointarray[j++]);
j++;
triangle.moveTo(pointarray[j], pointarray[j++]);
triangle.closePath();
g2.draw(triangle);
g2.fill(triangle);
}

}
}

• 03-22-2010, 01:02 AM
gcalvin
I'm not sure you're doing what you intend to do in this section:
Code:

int[] pointarray = { 36, 36, 0, 108, 36, 180,
36, 36, 0, 108, 36, 180, 180, 180, 216, 108, 180, 36};
...
for(i=0; i<3; i++)
{
GeneralPath triangle;
triangle = new GeneralPath();
triangle.moveTo(pointarray[j], pointarray[j++]); // moveTo(36, 36) - j now =1 and points to 36
j++; // j now= 2 and points to 0
triangle.moveTo(pointarray[j], pointarray[j++]); // moveTo(0, 0) - j now =3 and points to 108
j++; // j now =4 and points to 36
triangle.moveTo(pointarray[j], pointarray[j++]); // moveTo(36, 36) - j now =5 and points to 180
triangle.closePath();
g2.draw(triangle);
g2.fill(triangle);
}

-Gary-
• 03-22-2010, 01:07 AM
Fubarable
Don't draw directly in the JApplet but rather draw in a JPanel and then either place the JPanel in the JApplet's contentPane or as the JApplet's contentPane.
• 03-22-2010, 01:22 AM
That is a good idea fubarable but the rectangle part does draw I forgot to mention it. The array logic is supposed to take the next two points and put those points into the lineTo method.
• 03-22-2010, 01:25 AM
gcalvin
Quote:

Originally Posted by The_Sponzy_Paradox
That is a good idea fubarable but the rectangle part does draw I forgot to mention it. The array logic is supposed to take the next two points and put those points into the lineTo method.

It doesn't. In fact, I don't see any lineTo in your code at all, which may be something of an epiphany to you.

-Gary-
• 03-22-2010, 01:30 AM
Fubarable
Quote:

Originally Posted by The_Sponzy_Paradox
That is a good idea fubarable but the rectangle part does draw I forgot to mention it. The array logic is supposed to take the next two points and put those points into the lineTo method.

Even if it does draw, you lose all benefits of Swing graphics. Besides in Swing you should never draw directly on the applet or other top-level Swing components.
• 03-22-2010, 02:10 AM
Fubarable
Quote:

Originally Posted by gcalvin
It doesn't. In fact, I don't see any lineTo in your code at all, which may be something of an epiphany to you.

-Gary-

Yup, not to mention a too clever use of j++;
• 03-22-2010, 02:16 AM
Gcalvin, yeah I feel rather foolish, I guess I did not see the forest through the trees lol. oh well. And Fubarable it is just a test of the logic and algorithm for a gui project I am doing for class.
• 03-22-2010, 02:27 AM
Fubarable
Quote:

Originally Posted by The_Sponzy_Paradox
Gcalvin, yeah I feel rather foolish, I guess I did not see the forest through the trees lol. oh well. And Fubarable it is just a test of the logic and algorithm for a gui project I am doing for class.

That's fine, but I'm sure you're familiar with the difference between ++j and j++, correct? Regardless, you're far better off with a 3-D array.
Code:

class TriangleDrawPanel extends JPanel {
private int[][][] pointarray = {
{{36, 36}, {0, 108}, {36, 180}},
{{36, 36}, {0, 108}, {36, 180}}, // the same triangle??
{{180, 180}, {216, 108}, {180, 36}}};

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.green);
BasicStroke b;
b = new BasicStroke(2, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
g2.setStroke(b);

Rectangle2D.Float subset = new Rectangle2D.Float(36, 36, 144, 144);
// stand in for the Jframe that will eventually be there
g2.draw(subset);

for (int i = 0; i < pointarray.length; i++) {
Path2D triangle = new Path2D.Double();
for (int j = 0; j < pointarray[i].length; j++) {
int tx = pointarray[i][j][0];
int ty = pointarray[i][j][1];
if (j == 0) {
triangle.moveTo(tx, ty);
} else {
triangle.lineTo(tx, ty);
}
}
triangle.closePath();
g2.draw(triangle);
g2.fill(triangle);
}
}
}

• 03-22-2010, 03:42 AM
Quite true, however the proff said just a plain ole array of points. The assignment was to create four triangles in the shape of an octagon with a jframe in the center. The idea I had was run a for loop to control the number of triangles, then feed the array into that loop that uses the various statements moveTo() or line to two array spots at a time to actually draw them. I got it drawing but I still cannot get the points right. any ideas? here is what I got.

Code:

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;

public class coilgunguioctogonv201 extends JApplet
{
public  void paint(Graphics g)
{
int i, j = 0;

int[] pointarray = { 36, 36, 0, 108, 36, 180,
36, 180, 108, 216, 180, 180, 180, 180, 216,108, 36, 36, 180, 36, 108,0};
// points for the triangles

super.paint(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.green);
int x, y;
BasicStroke b;
b = new BasicStroke(2, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND);
g2.setStroke(b);

Rectangle2D.Float subset = new Rectangle2D.Float(36, 36, 144, 144);
//stand in for the Jframe that will eventually be there
g2.draw(subset);

for(i=0; i<3; i++)
{
GeneralPath triangle;
triangle = new GeneralPath();

x = pointarray[j];
j++;
y = pointarray[j];
j++        ;

triangle.moveTo(x,y);
x = pointarray[j];
j++;
y = pointarray[j];
j++        ;

triangle.lineTo(x,y);
x = pointarray[j];
j++;
y = pointarray[j];
j++;

triangle.lineTo(x,y);
x = pointarray[j];
j++;
y = pointarray[j];

if(j <= 21)
{
j++;
triangle.closePath();
g2.draw(triangle);
g2.fill(triangle);
}
else

{
triangle.closePath();
g2.draw(triangle);
g2.fill(triangle);
}

}
}
}

Left what it is supposed to be Right what I got. The one on the left is a test I made to get the points where I plotted every triangle to generate the points I needed