Results 21 to 24 of 24
Thread: Draw a pyramid
 11202011, 04:09 PM #21
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.
 11202011, 04:22 PM #22
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; 11202011 at 04:24 PM.
 11202011, 04:24 PM #23
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). ;)
 04022012, 01:44 PM #24Member
 Join Date
 Apr 2012
 Posts
 2
 Rep Power
 0
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=heightBRICK_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; 04022012 at 04:29 PM.
Similar Threads

Need Help With Some Pyramid
By lord raza in forum New To JavaReplies: 1Last Post: 11072011, 02:03 PM 
why cant i draw this box???
By stefandanielsen in forum New To JavaReplies: 2Last Post: 05122011, 02:53 PM 
need help with Number Pyramid with Double Digits
By SmellyFoot in forum New To JavaReplies: 5Last Post: 03292011, 12:04 PM 
pyramid
By Dashinator in forum New To JavaReplies: 8Last Post: 11152010, 10:10 PM 
Will make a pyramid of some kind out of a number
By romina in forum New To JavaReplies: 1Last Post: 08072007, 05:20 AM
Bookmarks