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 7079993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:44:24+00:00 2026-05-28T06:44:24+00:00

This is not a homework problem. I am only going over a freely available

  • 0

This is not a homework problem. I am only going over a freely available course from Stanford. I am using Ubuntu Linux with Eclipse.

Problem and Question:
I am drawing rectangles by calling add() on a acm.program.GraphicsProgram object. I am drawing certain number of rectangles which have a certain fixed width. However I am seeing that my rectangles are flowing off the visible area. I have tried setting a big enough width and height for both the GraphicsProgram object and the GCanvas object but still my rectangles are falling off the visible area. I always get the same height for GraphicsProgram object no matter what height I set. Any pointers as to what am I doing wrong?

    import acm.graphics.*;
    import acm.program.*;
    import java.awt.*;

    public class Pyramid extends GraphicsProgram {

    /** Width of each brick in pixels */
            private static final int BRICK_WIDTH = 30;

    /** Width of each brick in pixels */
            private static final int BRICK_HEIGHT = 12;

    /** Number of bricks in the base of the pyramid */
            private static final int BRICKS_IN_BASE = 14;

            public void run() {

                    setWindowSize();
                    this.createPyramid();
            }

            private void createPyramid()
            {
                    int centerX = findCenter();
                    int startingX = centerX - (BRICKS_IN_BASE / 2) * BRICK_WIDTH;
                    int startingY = BRICK_HEIGHT;

                    for(int numBricks = BRICKS_IN_BASE; numBricks>= 1; numBricks--)
                    {
                            this.layBricks(startingX,startingY , numBricks);
                            startingX = startingX + BRICK_WIDTH / 2;
                            startingY = (BRICKS_IN_BASE - numBricks + 2) * BRICK_HEIGHT;
                    }
            }

            private void layBricks(int x, int y, int numOfBricks)
            {
                    for(int i = 0; i < numOfBricks; i++)
                    {
                            add(new GRect(x,y,this.BRICK_WIDTH, this.BRICK_HEIGHT));
                            x+=this.BRICK_WIDTH;
                    }

            }
            private void setWindowSize()
            {
                    int width = BRICK_WIDTH * BRICKS_IN_BASE * 2;
                    int height = BRICKS_IN_BASE * BRICK_HEIGHT * 2;

                    this.setSize(width, height);
                    //this.setForeground(Color.GREEN);
                    //this.setBackground(Color.BLUE);
                    //this.getGCanvas().setBounds(0, 0, width, height);
                    //this.getGCanvas().add(new GRect(0,0,300,30));
                    //this.getGCanvas().setBackground(Color.WHITE);
                    System.out.println(this.getHeight());
                    System.out.println(this.getWidth());
                    System.out.println(this.getGCanvas().getHeight());
                    System.out.println(this.getGCanvas().getWidth());
            }


            private int findCenter()
            {

                    return this.getWidth() / 2;

            }


    }
  • 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-05-28T06:44:25+00:00Added an answer on May 28, 2026 at 6:44 am

    The main problem was that I was using open-java-jdk and not sun-java-jdk. After changing the jre my Applet is behaving in a more predictable way.

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

Sidebar

Related Questions

I'm trying to solve this problem, its not a homework question, its just code
This one is a case of not doing your homework.:-) Apart from dynamic loading
I have this homework, I have only 1 problem, and I don't know the
This is not a homework, it's more of a challenge from our prof but
This is not a homework problem, though it is a work problem. Where months
This is not homework, I need this for my program :) I ask this
Please note that this is not homework and i did search before starting this
this one is not homework, it just the fact that i've been out of
Disclaimer: This is for a homework assignment, but the question is not regarding the
Ok this is a homework questions, but I cannot find the answer anywhere, not

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.