Page 2 of 2 FirstFirst 12
Results 21 to 24 of 24

Thread: Draw a pyramid

  1. #21
    MisterNikos's Avatar
    MisterNikos is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: Draw a pyramid

    Aaaah, I found my mistake, but I don't know how to solve it.
    Here it is.
    My program gives 14 coordinates for i = 0, another 14 coordinates for i = 1, etc.
    How can I set my i for loop to run only ONCE for each j?
    Can you help me with this?
    I think if I do this, the pyramid will be ok.

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Draw a pyramid

    Think in terms of what row you are printing on and what column, not i and j.
    What is the x,y value for the first shape on the first row? How many shapes on this row?
    What is the x,y value for the first shape on the second row? How many shapes on this row?
    and so on for all the rows of shapes.

    Are you starting at the bottom and working up? Your y values are decreasing. I was starting at the top.
    The logic should be very similiar. For each row you need to compute the starting x value and the number of shapes to put on that row.
    Last edited by Norm; 11-20-2011 at 03:24 PM.

  3. #23
    MisterNikos's Avatar
    MisterNikos is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: Draw a pyramid

    You are right, Norm!
    Thank you very much for your help.
    I am satisfied even with the point that I have arrived to right now (beginner speaking). ;)

  4. #24
    Leandros is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: Draw a pyramid

    Hello I posted my code on another post but I just saw this one now so I thought I should repost it here.
    I hope it helps someone out there
    Java Code:
    import acm.graphics.*;
    import acm.program.*;
    
    public class Pyramid extends GraphicsProgram {
    
    	private static final int BRICK_WIDTH = 30;  /** Width of each brick in pixels */
    	private static final int BRICK_HEIGHT = 12;	/** Height of each brick in pixels */
    	private static final int BRICKS_IN_BASE = 14; /** Number of bricks in the base of the pyramid */
    	
    	public void run() {
    		int height=getHeight();
    		int width=getWidth();
    			
    		/** I want to manipulate the number of BRICKS_IN_NUMBER so I assign it to a variable double bricks*/
    		int bricks = BRICKS_IN_BASE;	
    		/** the first loop adds the same number of rows we have set for the number of bricks
    		 * the number can be changed by changing the BRICK_HEIGHT*/
    		for (int j=0; j<BRICKS_IN_BASE; j++){
    /**Its the same as doing height=height-BRICK_HEIGHT*/
    			height-=BRICK_HEIGHT;
    			int initPos=(width-(BRICK_WIDTH*bricks))/2;//calculates the initial position of each row
    
    			for (int i=bricks; i>0; i--){
    				/** set the size of the brick*/
    				GRect brick = new GRect(BRICK_WIDTH,BRICK_HEIGHT);
    				/** set the location of the brick*/
    				brick.setLocation(initPos, height);
    				add(brick);
    				/** this sets the position of the next brick*/
    				initPos+=BRICK_WIDTH;
    			}
    /**the subtraction of bricks must be executed after the first loop has placed the row of bricks otherwise the pyramid will not be centered*/
    			bricks--;
    
    		}
    	}
    }
    Last edited by Leandros; 04-02-2012 at 04:29 PM.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Need Help With Some Pyramid
    By lord raza in forum New To Java
    Replies: 1
    Last Post: 11-07-2011, 01:03 PM
  2. why cant i draw this box???
    By stefandanielsen in forum New To Java
    Replies: 2
    Last Post: 05-12-2011, 02:53 PM
  3. need help with Number Pyramid with Double Digits
    By SmellyFoot in forum New To Java
    Replies: 5
    Last Post: 03-29-2011, 12:04 PM
  4. pyramid
    By Dashinator in forum New To Java
    Replies: 8
    Last Post: 11-15-2010, 09:10 PM
  5. Will make a pyramid of some kind out of a number
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:20 AM

Posting Permissions

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