Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2010
    Location
    Troy Upstate New York USA (Not in New York City)
    Posts
    25
    Rep Power
    0

    Question 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.

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    I'm not sure you're doing what you intend to do in this section:
    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);
    		}
    -Gary-

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  4. #4
    Join Date
    Mar 2010
    Location
    Troy Upstate New York USA (Not in New York City)
    Posts
    25
    Rep Power
    0

    Default

    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.

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by The_Sponzy_Paradox View Post
    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-

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by The_Sponzy_Paradox View Post
    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.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by gcalvin View Post
    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++;

  8. #8
    Join Date
    Mar 2010
    Location
    Troy Upstate New York USA (Not in New York City)
    Posts
    25
    Rep Power
    0

    Default

    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.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by The_Sponzy_Paradox View Post
    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.
    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);
        }
      }
    }

  10. #10
    Join Date
    Mar 2010
    Location
    Troy Upstate New York USA (Not in New York City)
    Posts
    25
    Rep Power
    0

    Default

    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.

    Java 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
    Attached Thumbnails Attached Thumbnails Triangles wont draw Help?-octagon.jpg  
    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

  1. Triangles java problem (basic help)
    By adz06 in forum New To Java
    Replies: 5
    Last Post: 10-31-2009, 06:58 PM
  2. why wont it compile
    By bje98f in forum Advanced Java
    Replies: 1
    Last Post: 04-23-2009, 10:55 PM
  3. Drawing out triangles
    By leiferouis in forum New To Java
    Replies: 24
    Last Post: 01-16-2009, 08:18 PM
  4. Triangles
    By CodeDog in forum New To Java
    Replies: 9
    Last Post: 10-14-2008, 09:18 PM
  5. asterisks triangles
    By Dan121 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 07:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •