[source lang="java"] for(int i = 0; i < BRICKS.size(); i++) { BRICKS.paintBrick(g); }[/source]
at the BRICKS.paintBrick(g); line i get an error saying that the method paintBrick is undefined for ArrayList<Brick>
im not exactly sure why, i think i created the arraylist right
public ArrayList<Brick> BRICKS;
BRICKS = new ArrayList<Brick>();
and i have the paintBrick method in my class Brick
6 replies to this topic
Sponsor:
#4 Members - Reputation: 593
Posted 25 August 2012 - 01:50 PM
You can use "List.get(...)", to return the element at a given index in the ArrayList.
[source lang="java"] for(int i = 0; i < BRICKS.size(); i++) { BRICKS.get(i).paintBrick(g); }[/source]
Generic collections can also take advantage of the For-Each loop.
E.g.
[source lang="java"] for (final Brick b : BRICKS) { b.paintBrick(g); }[/source]
[source lang="java"] for(int i = 0; i < BRICKS.size(); i++) { BRICKS.get(i).paintBrick(g); }[/source]
Generic collections can also take advantage of the For-Each loop.
E.g.
[source lang="java"] for (final Brick b : BRICKS) { b.paintBrick(g); }[/source]
Edited by Angex, 25 August 2012 - 01:51 PM.
#5 Members - Reputation: 264
Posted 25 August 2012 - 03:56 PM
ah your first solution worked.
another problem now.
[source lang="java"] public Rectangle getBrickBounds() { for(int i = 0; i < BRICKS.size(); i++) { return new Rectangle(Brick.x, Brick.y, Brick.width, Brick.height); } }[/source]
im returning a rectangle for each brick in the BRICKS arraylist, but its saying that the variables im returning are not static whatever that means
another problem now.
[source lang="java"] public Rectangle getBrickBounds() { for(int i = 0; i < BRICKS.size(); i++) { return new Rectangle(Brick.x, Brick.y, Brick.width, Brick.height); } }[/source]
im returning a rectangle for each brick in the BRICKS arraylist, but its saying that the variables im returning are not static whatever that means
#7 Moderators - Reputation: 5305
Posted 26 August 2012 - 03:54 AM
If you want to return a rectangle for each brick, you need to return an (array)list of rectangles. This could be populated using a foreach loop as Angex demonstrated.
However, it seems you have some fundamental weaknesses in your current understanding of the Java language and in the kind of problem solving you need to be able to do to program effectively. I think you would benefit by putting this graphical application on hold for a while, and write some text mode games to get familiarity with the basic language. You can return to this project when you are comfortable with the core language.
Consider investing in a good book, or borrowing one from your local library, and reading it through and following all the exercises at the end of each chapter.
However, it seems you have some fundamental weaknesses in your current understanding of the Java language and in the kind of problem solving you need to be able to do to program effectively. I think you would benefit by putting this graphical application on hold for a while, and write some text mode games to get familiarity with the basic language. You can return to this project when you are comfortable with the core language.
Consider investing in a good book, or borrowing one from your local library, and reading it through and following all the exercises at the end of each chapter.






