Results 1 to 10 of 10
Thread: Triangles wont draw Help?
- 03-21-2010, 11:37 PM #1
Member
- Join Date
- Mar 2010
- Location
- Troy Upstate New York USA (Not in New York City)
- Posts
- 25
- Rep Power
- 0
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.
Java 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); } } }You know that line between genius and insanity? I am told I crossed it and went around back to insanity again.
- 03-22-2010, 12:02 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
I'm not sure you're doing what you intend to do in this section:
-Gary-Java 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); }
-
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, 12:22 AM #4
Member
- Join Date
- Mar 2010
- Location
- Troy Upstate New York USA (Not in New York City)
- Posts
- 25
- Rep Power
- 0
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.
You know that line between genius and insanity? I am told I crossed it and went around back to insanity again.
- 03-22-2010, 12:25 AM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
-
-
- 03-22-2010, 01:16 AM #8
Member
- Join Date
- Mar 2010
- Location
- Troy Upstate New York USA (Not in New York City)
- Posts
- 25
- Rep Power
- 0
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.
Last edited by The_Sponzy_Paradox; 03-22-2010 at 01:20 AM.
You know that line between genius and insanity? I am told I crossed it and went around back to insanity again.
-
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.
Java 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, 02:42 AM #10
Member
- Join Date
- Mar 2010
- Location
- Troy Upstate New York USA (Not in New York City)
- Posts
- 25
- Rep Power
- 0
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.
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 neededJava 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); } } } }Last edited by The_Sponzy_Paradox; 03-22-2010 at 03:34 AM. Reason: I got some pictures
You know that line between genius and insanity? I am told I crossed it and went around back to insanity again.
Similar Threads
-
Triangles java problem (basic help)
By adz06 in forum New To JavaReplies: 5Last Post: 10-31-2009, 06:58 PM -
why wont it compile
By bje98f in forum Advanced JavaReplies: 1Last Post: 04-23-2009, 10:55 PM -
Drawing out triangles
By leiferouis in forum New To JavaReplies: 24Last Post: 01-16-2009, 08:18 PM -
Triangles
By CodeDog in forum New To JavaReplies: 9Last Post: 10-14-2008, 09:18 PM -
asterisks triangles
By Dan121 in forum New To JavaReplies: 1Last Post: 01-12-2008, 07:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks