0

So I'm relatively new to Java, and I've been working with Eclipse. The main part of this is the last three lines, I want to have the object myBrick appear multiple times. If I run this, it only appears in the second location (the last line). Is it possible?

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;

    /** Height 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() {
        double windowWidth = 756;
        double windowHeight = 494;
        int numberOfBricks = (BRICKS_IN_BASE*(BRICKS_IN_BASE+1))/2;

        double emptySpace = windowWidth - (BRICK_WIDTH*BRICKS_IN_BASE);
        int cushion = (int)emptySpace/2;

        GRect myBrick = new GRect(BRICK_WIDTH, BRICK_HEIGHT);
        add(myBrick, cushion, (windowHeight-BRICK_HEIGHT));
        add(myBrick, (cushion+BRICK_WIDTH), (windowHeight-BRICK_HEIGHT));
    }
}
7
  • whats the add() function here? Commented Feb 11, 2013 at 5:22
  • I believe it adds the object to the window Commented Feb 11, 2013 at 5:23
  • Appear multiple times where? Commented Feb 11, 2013 at 5:28
  • If you are new to java.write you programs in notepad and run them using the command line...Just a suggestion Commented Feb 11, 2013 at 5:32
  • I want it to appear multiple times on the graphics window that appears. Commented Feb 11, 2013 at 5:38

3 Answers 3

2

I think the problem might be when you use the second add() method it is using the same myBrick object instead of creating a second one. Try making a new GRect object for your second add() function.

Sign up to request clarification or add additional context in comments.

3 Comments

That's the thing, I want to add the same object multiple times.
Without looking at the add() it's difficult to find out the problem. It might be possible that the add() method is overwriting the previous values with new ones when called.
I think that's the case... is there a better method than add()?
0

The second add() is using the same object reference as the first. Java is a pass-by-value language, but it passes the value of the reference to objects. That means that whatever mutations the first add() makes to myBrick will be seen by the second add() call, which I am sure violates the semantics of the program. (I suggest you read this SOF post)

The construction of the GRect object doesn't seem to be too complicated, so why not duplicate this instruction and use it for each of the add() call's first parameters? This way, you ensure that you have two GRect objects starting with the same state, but modifying one will not affect the other.

2 Comments

I have an indefinite number of these GRects and they need to be in different places
Write a loop that creates a new GRect object for each call to add().
0

As others have pointed out, without looking at add method, it's difficult to give you the answer. But If i am not wrong, may be you are looking for something like this,

  1. Create a list of that object. List rectList = new ArrayList();
  2. Create new Object of GRect and then call the add function.
  3. add the new GRect object to rectList.

This way in the list you will have three GRect Objects. You can iterate and get the values of the GRect object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.