Reputation: 25
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()?
Upvotes: 0
Views: 603
Reputation: 5239
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.
Upvotes: 0
Reputation: 44834
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);
Upvotes: 1