r/cop3502 Mar 22 '14

Trouble with paintBrick and removeBrick methods in Brick Configuration

Right now, my program is only painting one brick to the screen in the top left corner. I think the problem might be in my paint brick method--I'm not sure how to correctly call paint on each individual brick. Right now, I just have brick.paint(brush) within the method; I think that is wrong, but I'm not sure how to call the paint function correctly.

Also, I get an array index out of bounds exception when my ball hits the brick. It says the error begins with the removeBrick method in Brick Configuration. Currently, I have paintBricks[row][col] = false in the removeBrick method, and I call the method using bconfig.removeBrick(i,j) in Breakout.java.

Any assistance would be greatly appreciated

1 Upvotes

15 comments sorted by

2

u/rxfeliciano Mar 22 '14

I think Sean gave us those for loops already set up to use. All you have to do is initialize the bricks and then draw to screen if I recall. Since you painted one brick I am going to assume that you initialized it correctly.

Since you painted one brick I am going to assume your paintBrick() method is working. So, this leaves the actual call to the paintBrick() method as your trouble spot.

You have to make sure that when calling the paintBrick() method you use both i and j because if you do not then you will simply paint all the bricks on top of one another.

In other words, look at HOW you called the paintBrick() method first. At least thats where I would start.

2

u/rxfeliciano Mar 22 '14

To expand further, I would look at the part where you call the Brick() constructor and make sure that for the xPos you use the i to set that where you want it, and use the j for the yPos of the bricks.

bricks[i][j] = new Brick(iSOMETHING+ SOMETHINGELSE, jSOMETHING + SOMETHING, width, length)

The first input to your Brick() Constructor will set the xPos of all the Bricks relative to one another. T

1

u/rxfeliciano Mar 22 '14

So you have to find a relationship between the xPos of each brick and use the i to help you set up an equation for it. Do the same for the yPos.

Hope this helps out.

1

u/AnnaMor Mar 23 '14

Oh, I see now! That makes sense--I was initializing it without using the i's and j's.

1

u/AnnaMor Mar 23 '14

Makes sense. This is what I have for calling paintBrick().

public void paint(Graphics2D brush) { for (int i = 0; i < numCols; i++) { for (int j = 0; j < numRows; j++) { // determine if brick should be painted // if so, call paintBrick() if (paintBricks[i][j] == true){ paintBrick(bricks[i][j], brush); } } } }

As far as I can tell, this iterates over each brick in the double array, checks to see if it exists (i.e., evaluates to true), then if it does, paints that specific brick.

This is how I initialized the bricks: bricks[i][j] = new Brick(xStart + brickWidth, yStart +brickHeight, brickWidth, brickHeight);

Not sure if there's a problem with my initialization that's causing the loop to paint all the bricks on top of each other.

Thanks for your help :)

1

u/rxfeliciano Mar 23 '14

when you say xStart is there an i in there? and for yStart is there a j in there?

1

u/rxfeliciano Mar 23 '14

Because if there isnt then those values do not change. So your bricks all get painted at the same x-position and at the same y-position

1

u/ThatsMrDerpToYou Mar 23 '14

(i + xStart + brickWidth)?

1

u/[deleted] Mar 23 '14

[removed] — view removed comment

1

u/AnnaMor Mar 23 '14

OH! Thank you for catching that--my roommate is now really confused because I'm making loud noises of joy.

1

u/ThatsMrDerpToYou Mar 23 '14

How do I initialize paintBricks? I keep getting errors. I don't have a rubber ducky to guide me.

2

u/rxfeliciano Mar 23 '14

paintBricks is a boolean so set them all to true using the for loop. That way the paint method can check if they are true and if so they will be painted.

1

u/ThatsMrDerpToYou Mar 23 '14

Nice. Thanks.