0

How can I reference the x-position of the first ball and use that on the second ball?

private List<Ball> createBalls() {
    List<Ball> balls = new ArrayList<>();

    balls.add(new Ball(Color.BLUE, 600, 400, 20, rInt(-15,15), rInt(-15,15)));
    balls.add(new Ball(Color.RED, ball(1).get position 1, 500, 13, rInt(-15,15), rInt(-15,15)));
    balls.add(new Ball(Color.GREEN, 320, 340, 20, rInt(-15,15), rInt(-15,15)));
    balls.add(new Ball(Color.YELLOW, 50, 400, 50, rInt(-15,15), rInt(-15,15)));
    balls.add(new Ball(Color.orange, 600, 300, 15, rInt(-15,15), rInt(-15,15)));
    balls.add(new Ball(Color.CYAN, 320, 340, 10, rInt(-15,15), rInt(-15,15)));
    balls.add(new Ball(Color.GRAY, 50, 400, 60, rInt(-15,15), rInt(-15,15)));
    balls.add(new Ball(Color.magenta, 320, 340, 25, rInt(-15,15), rInt(-15,15)));
    balls.add(new Ball(Color.white, 50, 400, 40, rInt(-15,15), rInt(-15,15)));
    balls.add(new Ball(Color.YELLOW, 800, 200, 500, rInt(-15,15), rInt(-15,15)));

    return balls;
}

In my ball class I have

    public double getPositionX() {
        return positionX;
    }

    public void setPositionX(double positionX) {
        this.positionX = positionX;
    }
19
  • 1
    "Position value of the X position value for the next element position value?" What? Commented Oct 5, 2016 at 18:00
  • 1
    I think you are looking for a way to increment the x position adding onto previous balls x position. For that you need to store the previous ball to variable Ball prev = new Ball(Color.BLUE, 600, 400, 20, rInt(-15,15), rInt(-15,15)) then in second do something like prev.getPositionX() assuming you have getPositionX() in Ball class that returns its X position. Commented Oct 5, 2016 at 18:00
  • 3
    Your question is not clear reorganize it. Commented Oct 5, 2016 at 18:00
  • position value of the position value for the next position value? pls clarify what do you mean. Commented Oct 5, 2016 at 18:01
  • I am clarifying the quetion now thanks Commented Oct 5, 2016 at 18:02

1 Answer 1

1

Not sure I understand but to get a value of X (assuming there is a Ball.getX(). then: balls.get(1).getX();

"for the next element position value?"

You can't get the "next element" before you create it. So after you create the Balls loop though the list and apply the "get next X value" logic.

If you show more code and explain what you are trying to do a little more clearly it would help.

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

4 Comments

So I changed it and I think it works now but it won't let me run and I have no errors in code
at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
That stack trace you provided doesn't help, normally it would have line numbers or at least show one of your classes in the trace. What is the Exception?
@Alzoid Its IndexOutOfBoundsException. Check the comments on question. And the first ball should be accessed by index 0 not 1.

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.