• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pyramid in Graphics Program Java

 
Ranch Hand
Posts: 149
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys, I'm new to Java, so a little help would be appreciated highly Anyway, I have an assignment from Stanford's lectures (I'm following lectures from YT).

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.

1.png
[Thumbnail for 1.png]
 
Saloon Keeper
Posts: 15488
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Miljan, welcome to CodeRanch!

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.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about a version where no calculation is needed?

Hint: by default, a FlowLayout centers , say, an opaque JLabel, no matter how many of these labels are in a JPanel.
 
Stephan van Hulst
Saloon Keeper
Posts: 15488
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think the assignment allows for that, because it involves an applet-like framework in which you need to draw graphics, not layout components.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not see any requirement that forbids what I am hinting.
 
Stephan van Hulst
Saloon Keeper
Posts: 15488
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Write a Graphics Program subclass that draws a pyramid".

GraphicsProgram is a class from the Stanford lectures.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see...

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.
 
Mike Gosling
Ranch Hand
Posts: 149
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Stefan

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.

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

- 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 ?

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

Ah well, it's calculation time, then.

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 ?


I apologize up front if I am not using correct terminology or asking too much (a bit undefined) questions.
 
Stephan van Hulst
Saloon Keeper
Posts: 15488
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, maybe size wasn't the right word. I meant a pyramid of height 1, 2 etc.

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?
 
Mike Gosling
Ranch Hand
Posts: 149
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The pyramid should be in the center of canvass, whatever the the size of pyramid is, and by centering it I also mean to make it look like the pyramid from the picture. I still cannot set the appropriate coordinates to make it look like the pyramid from the picture, that is perhaps the biggest issue here.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Miljan,

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!
 
Stephan van Hulst
Saloon Keeper
Posts: 15488
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, let's focus on that first.

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.
 
Mike Gosling
Ranch Hand
Posts: 149
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys, I was soo busy these last week that I completely forgot to reply.

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
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Miljan,

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.
 
Rancher
Posts: 285
14
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i made a small game last week that draws graphics, and i found the graphic method for drawing rectangles/squares does NOT draw from the center as you would expect it to do. so you have to wrap it inside a method of your own that ensures it draws from center. once you do that, you can simply calculate the RELATIVE positions based on their origin points at the center of each brick. you need to decide what you are drawing them in relation to: the center of a JPanel? the distance off a simulated floor in the center of a simulated room? start small, use 3 rows to test your methods. Heres an example, stars represent origins:



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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic