0

Attempting to get a custom class known as DrawGraphics to contain an ArrayList of custom objects instead of a single Sprite. However the ArrayList refuses to accept the new Bouncer object, and when it does, the rest of the DrawGraphics class does not recognize it.

Original Code

package objectssequel;

import java.util.ArrayList;
import java.awt.Color;
import java.awt.Graphics;

public class DrawGraphics {
    Bouncer movingSprite;  //this was the original single sprite

    /** Initializes this class for drawing. */
    public DrawGraphics() {
        Rectangle box = new Rectangle(15, 20, Color.RED);
        movingSprite = new Bouncer(100, 170, box);
        movingSprite.setMovementVector(3, 1);
    }

    /** Draw the contents of the window on surface. */
    public void draw(Graphics surface) {
    movingSprite.draw(surface);
    }
}

Attempted Solution: First I created the ArrayList of objects of class Bouncer

ArrayList<Bouncer> bouncerList = new ArrayList<Bouncer>();

All good. I first then inserted the following code on the line below this

bouncerList.add(movingSprite);

This generated a "Syntax error on token(s), misplaced construct(s)" and "Syntax error on token "movingSprite", VariableDeclaratorId expected after this token" compiler error. I guessed this might be because I used the bouncerList.add() outside of a method body, so I created the following method for the class DrawGraphics

    public void addBouncer(Bouncer newBouncer) {
        bouncerList.add(newBouncer);
    }

I then called this method in DrawGraphics() with:

addBouncer(movingSprite);

the compiler error informed me that movingSprite could not be resolved to a variable type. I attempted this:

 public void addBouncer() {
        Bouncer movingSprite;
        bouncerList.add(movingSprite);
    }

Then attempted to initialize movingSprite by giving it a null setting, and no such luck as well, and probably a dozen other combinations ways to fix this. Any solutions? How do I create an ArrayList of Bouncer objects within the DrawGraphics Class?

Edit: Is it possible to not use and remove 'Bouncer movingSprite' from the original code and create an instance of the object just from the bouncerList.add()?

2 Answers 2

1

In this code

public void addBouncer(Bouncer newBouncer) {
        bouncerList.add(Bouncer);               // this is trying to add a class
    }

you need to change to

public void addBouncer(Bouncer newBouncer) {
    bouncerList.add(newBouncer);             // this will add the object
}

and after

  movingSprite.setMovementVector(3, 1);

call

  addBouncer (movingSprite);
Sign up to request clarification or add additional context in comments.

4 Comments

Ahh yes that was a typo when entering my question. I have bouncerList.add(newBouncer) in there. Calling addBouncer(movingSprite) after the movement vector does work, however is there a different way to create the array list and add the object and remove the original 'Bouncer movingSprite' in the original code?
call bouncerList.remove (oldBouncer)
Poor wording on my part. What I meant was can I create the array list objects without using the original 'Bouncer movingSprite;' code in the first place?
You can but you are using movingSprite in the draw method. Where would that come from?
0

You are trying to declare and initialize the array at object construction time? Sadly, java collections makes this seemingly obvious use case awkward.

List< Bouncer > bouncerList = new ArrayList< Bouncer >() { {
    add( new Bouncer() );
} };

This may lead to difficulty serializing your DrawGraphics class if ever that becomes necessary. Why not populate it in the DrawGraphics constructor?

Another option:

List< Bouncer > bouncerList = new ArrayList< Bouncer >( Arrays.asList( new Bouncer() ) );

You can also use guava's Lists utility class to construct and populate an array list in a single line.

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.