Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8882349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:33:21+00:00 2026-06-14T20:33:21+00:00

I’m working on a problem I’m having lots of trouble with. The concept of

  • 0

I’m working on a problem I’m having lots of trouble with. The concept of the question is to build a pyramid using bricks. The entire pyramid of bricks is centered in the window. I can draw one brick, then two, then three all the way up until 12 which makes up the base of the pyramid but all of the bricks are alined on the left edge on the left of the window instead of being centered in the window.

Using getWidth() and getHeight() i can do (getWidth()-BRICK_WIDTH) / 2; to get the center for the x coordinate of the brick. And then (getHeight() -BRICK_HEIGHT) / 2; for the center of the y coordinate of one brick. The only problem is i don’t understand where to enter that code so it applies to all the bricks so each row of bricks is centered in the window.

import acm.program.*;
import acm.graphics.*;

public class Pyramid extends GraphicsProgram {
  public void run() {
    double xCoordinate = (getWidth() - BRICKWIDTH) / 2;
    double yCoordinate = (getHeight() - BRICK_HEIGHT / 2);
    for (int i = 0; i < BRICKS_IN_BASE; i++) {
      for (int j = 0; j < i; j++) {
        double x = j * BRICK_WIDTH;
        double y = i * BRICK_HEIGHT;
        GRect square = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
        add(square);
      }
    }
  }
  private static final int BRICK_WIDTH = 50;
  private static final int BRICK_HEIGHT = 25;
  private static final int BRICKS_IN_BASE = 12;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:33:22+00:00Added an answer on June 14, 2026 at 8:33 pm

    You should try something like this :

    import acm.program.*;
    import acm.graphics.*;
    
    public class Pyramid extends GraphicsProgram
    {
        public void run()
        {
            // We calculate some values in order to center the pyramid vertically
            int pyramidHeight = BRICKS_IN_BASE * BRICK_HEIGHT;
            double pyramidY = (getHeight() - pyramidHeight) / 2;
    
            // For each brick layer...
            for (int i=BRICKS_IN_BASE ; i >= 1; i--)
            {
                // We calculate some values in order to center the layer horizontally
                int layerWidth = BRICKWIDTH * i;
                double layerX = (getWidth() - layerWidth) / 2;
                double layerY = pyramidY + (i-1) * BRICK_HEIGHT;
    
                // For each brick in the layer...
                for(int j=0 ; j<i ; j++)
                {
                    GRect square = new GRect(layerX + j*BRICK_WIDTH, layerY, BRICK_WIDTH, BRICK_HEIGHT);
                    add(square);
                }
            }
        }
    
        private static final int BRICK_WIDTH = 50;
        private static final int BRICK_HEIGHT = 25;
        private static final int BRICKS_IN_BASE = 12;
    }
    

    In this implementation, we first calculate the global width of the layer (because we already know how many bricks there will be in it) and we use it to find a global “point of start” of the layer, from which we find all the coordinates for all the rectangles.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using JSon response to parse title,date content and thumbnail images and place
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I've tracked down a weird MySQL problem to the two different ways I was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.