Write a Graphics Program subclass that draws a pyramid consisting of bricks arranged in horizontal rows, so that the number of bricks in each row decreases by one as you move up the pyramid, as shown in the following sample run (Attachment).

The pyramid should be centered at the bottom of the window and should use constants for the following parameters:

BRICK_WIDTH The width of each brick (30 pixels)

BRICK_HEIGHT The height of each brick (12 pixels)

BRICKS_IN_BASE The number of bricks in the base (14).

This is my code, I did it as the best I could:

What I don't get is how to center that whole pyramid in Graphics Program ? I have also found the same problem with following answer(s) at StackOverFlow (http://stackoverflow.com/questions/7191450/exercise-write-a-graphics-program-that-draws-a-pyramid ; you will notice that my code is almost identical to the one offered at StackOverFlow), but I don't understand that answer, that is, I don't know how to implement that answer as a code.

I don't know how to make that "area" for the pyramid and what maybe is the most important issue that worries me, is how to set the coordinates right, so every new line of bricks has x coordinate bigger for WIDTH_BRICK/2.

It would help me a lot if someone can explain to me in "plain words" or to write the whole code. I am struggling with this problem several days now.

How far to the right does the top brick need to be for a pyramid of size 1? A pyramid of size 2?

You will see that the x coordinate depends on the height of the layer. There are two ways you can solve this problem:

1) Build the pyramid from the bottom up, rather than top down. For each layer, you can add half a brick's width to the x coordinate.

2) First calculate the height of the current layer, and then add the correct amount of pixels to the x coordinate.

*The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.*

Hint: by default, a FlowLayout centers , say, an opaque JLabel, no matter how many of these labels are in a JPanel.

*The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.*

`GraphicsProgram`is a class from the Stanford lectures.

*The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.*

Just looked up that GraphicsProgram. Seems a bit old, and what a pity: this flowlayout thingy works a treat, with no calculations at all.

With some imagination can you put that panel as a component (method add(Component, ...) but that second parameter is a GPoint.

Ah well, it's calculation time, then.

Hi Miljan, welcome to CodeRanch!

Thanks Stefan,

When you say for a pyramid of size 1, size of 2. I have no idea what that means. I dont' know how to define "size“ of pyramid. I just know that I have to make certain area where I am going to put that pyramid in and to center that pyramid according to the size of that area. Also, methods like getWidth() and getHeight() could be useful for something like that, but again I have no idea how to use them. I generally have I solid idea WHAT I want to do, but don’t know how to express that in code.

- height of every row is BRICK_HEIGHT just adding up, I have that in my for loop. I have tried in several different ways to make corrections in the for loop but its not working. Am I supposed to calculate where the bottom row will begin at (by that I mean its coordinates), and then for every next row just to add BRICK_WIDTH/2 for x axis. How to do that in for loop ?First calculate the height of the current layer, and then add the correct amount of pixels to the x coordinate.

@ Piet Souris

No Piet, we are not supposed to use your way of doing the assignment at this stage, we need to do it the slow way I guess.

Do you mean by that to calculate where are x and y coordinates for bottom row and then for every next every row to add BRICK_WIDTH/2 for x axis and BRICK_HEGHT for y axis ?Ah well, it's calculation time, then.

I apologize up front if I am not using correct terminology or asking too much (a bit undefined) questions.

I don't understand what you mean by centering a pyramid according to the size of an area. Do you mean you can already draw the pyramid correctly, but you need to align it in the middle of whatever canvas you need to draw it inside of?

since I am not familiar with this GraphicsProgram (lets call it 'GP"), I can only give some hints.

First, there is a method available in GP, that gives the size of the center zone (it reminds me of a BorderLayout). See the API.

Now, suppose it is 200 pixels wide, and 200 high..

A brick is say 40 pixels wide and 12 high. Suppose we need to create a row of 14 bricks.

First: the total width of theze bricks is: 14 * 40 = 560 picels.

We must center these in the canvas, with its width of 200.

A little math shows that the first brick must start at (200 - 560) / 2 = -180.

Now, suppose also that the coordinate system has its bottom left at position (0,0) and its upper left at (0, 199).

Our first leftmost brick must now be added at location (-180, 0).

The brick at the right of it, has its x-coordinate equal to -180 + width of a brick = -140. So, that brick goes in position (-140, 0). Et cetera, until we had all 14 bricks.

The row above contains 13 bricks. Now, the y vcoordinates will be at 0 + 1 * 12 = 12 for all the bricks on that row.

We must also calculate the x-coordinates.

Two ways to do that:

1) we do the same maths as before: (200 - 13 * 40) = -160, et cetera,

2) the bricks are all a half brick shifted to the right, compared to the row below. So, the first brick starts at (-160, 12).

Et cetera!

Unfortunalety: if the canvas changes size (maybe if the frame is resized), we must do all our calculations again, taking the new horizontal size into account! Otherwise, the pyramid will not stay centered.

Keep us informed!

First we use some more descriptive names for your variables:

It's standard practice in Java to start counting at 0. Here, layer 0 means the top of the pyramid, and brick 0 means the leftmost brick in a layer. The height of a pyramid is the same as the number of bricks in its base. The width of a layer is the same as its index + 1. For instance, the top of any pyramid, being at layer index 0, has 1 brick in it. Because we made aliases for "height" and "width", it becomes much more clear what the code is doing.

Now, when you draw a brick in a layer, it has to moved to the right a little bit, depending on how much distance there is between the current layer and the base layer. For instance, there is no distance between the base layer and the base layer, so bricks in the base layer don't have to move extra to the right.

It's up to you to determine the distance between the current layer and the base layer, and then determine the x and y coordinates where the brick should be drawn. Don't worry about centering the pyramid yet.

I have tried your suggestions, looking and searching on the web, thinking about the assignment and your posts (holistic and reductionist approach ) but I couldn't dо it

Thanks for all the suggestions and taking your time to deal with newbie guy, but I will continue with further reading the book and listening the lectures. I'm wasting too much time on this one. If you maybe know how to do entire code, just post it here

Miljan Puletic wrote:If you maybe know how to do entire code, just post it here

I'm sorry, but we can't do that - we are not a code mill.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

okay, give it some time then. I guess it really is not as simple as we might have suggested it was.

Anyway: I was talking about a solution that needed no calculation at all, just some layout juggling.

Since this code has nothinhg to do with what you are supposed to write, I might as well give that tiny solution. Hope you like it.

you could also do as piet suggests and draw each brick to a seperate Jpanel and rely on a layout manager to arrange it. putting each row into a box with horizontal glue, and multiple rows into maybe a flow layout or gridbag would work good.