Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## bricks wierd problem

I am programming the game bricks, and i have a wierd problem: the ball only hits bricks above a curtain line.
ball code:
```import java.awt.Graphics;

public class Ball {
private int r;
private int centerX;
private int centerY;
private boolean isLeft;
private boolean isUp;

public Ball(int r, int centerX, int centerY) {
this.r = r;
this.centerX = centerX;
this.centerY = centerY;
isLeft = false;
isUp = true;
}

public void check(Brick[][] b) {
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b.length; j++) {
if (b[i][j] != null) {
for (int k = 0; k <= b[i][j].getWidth(); k++) {
if (contains(b[i][j].getX() + k, b[i][j].getY())
|| contains(b[i][j].getX() + k, b[i][j].getY()
- b[i][j].getHeight())) {
b[i][j] = null;
isUp = !isUp;
return;
}
}
for (int k = 0; k <= b[i][j].getHeight(); k++) {
if (contains(b[i][j].getX(), b[i][j].getY() - k)
|| contains(
b[i][j].getX() + b[i][j].getWidth(),
b[i][j].getY() - k)) {
b[i][j] = null;
isLeft = !isLeft;
return;
}
}
}
}
}
}

public void draw(Graphics g) {
g.fillOval(centerX - r, centerY - r, 2 * r, 2 * r);
}

public int getR() {
return r;
}

public void setR(int r) {
this.r = r;
}

public int getCenterX() {
return centerX;
}

public void setCenterX(int centerX) {
this.centerX = centerX;
}

public int getCenterY() {
return centerY;
}

public void setCenterY(int centerY) {
this.centerY = centerY;
}

public boolean isLeft() {
return isLeft;
}

public void setLeft(boolean isLeft) {
this.isLeft = isLeft;
}

public boolean isUp() {
return isUp;
}

public void setUp(boolean isUp) {
this.isUp = isUp;
}

public void move() {
if (isLeft)
centerX += 5;
else
centerX -= 5;
if (isUp)
centerY -= 5;
else
centerY += 5;
}

public boolean contains(int x, int y) {
double dis = Math.sqrt(Math.pow((centerX - x), 2)
+ Math.pow((centerY - y), 2));
if (dis <= r)
return true;
return false;
}

public void move(int x, int y) {
centerX = x;
centerY = y;
}
}```

brick code:
```import java.awt.Graphics;
public class Brick {
private int x;
private int y;
private int width;
private int height;

public Brick(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;

}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}

public void draw(Graphics g) {
g.fillRect(x, y, width, height);
}

public boolean contains(int x, int y) {
if (x >= this.x && x <= this.x + width && y >= this.y
&& y <= this.x + height)
return true;
return false;
}```

main code:
```package bricks;

import java.awt.Graphics;
import java.util.Random;
import javax.swing.*;

public class bounce extends JPanel {
private Ball ball;
private Brick[][] bricks;
private int width;
private int height;
public bounce() {
width=900;
height=700;
Random r = new Random();
ball = new Ball(10, 400, 500);
bricks = new Brick[5][8];
for (int i = 0; i < bricks.length; i++) {
for (int j = 0; j < bricks[i].length; j++) {
if (r.nextBoolean()) {
bricks[i][j] = new Brick(10 + i * 60, 10 + j * 35, 50, 25);
}
}
}

@Override
public void run() {
while (true) {
repaint();
ball.check(bricks);
try {
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
t.start();
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.clearRect(0, 0, width, height);
g.drawRect(0, 0, width, height);
for (int i = 0; i < bricks.length; i++)
for (int j = 0; j < bricks[i].length; j++)
if (bricks[i][j] != null)
bricks[i][j].draw(g);
ball.draw(g);
ball.move();
if (ball.contains(0, ball.getCenterY())
|| ball.contains(width, ball.getCenterY())) {
ball.setLeft(!ball.isLeft());
}
if (ball.contains(ball.getCenterX(), 0)
|| ball.contains(ball.getCenterX(), height)) {
ball.setUp(!ball.isUp());
}
}

public static void main(String[] args) {
JFrame f = new JFrame();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(0, 0, 1000, 1000);
}

}```

2. ## Re: bricks wierd problem

the ball only hits bricks above a curtain line.
Sounds like a logic problem.
Have you tried debugging the code by adding printlns to show the values of the variables that control what is happening? Print them out every time they change and when they are used to decide when the ball hits the curtain.

If you want anyone to test your code you need to post code that compiles.
Your posted code does not compile.

3. ## Re: bricks wierd problem

i forgot the imports. it should work now.

4. ## Re: bricks wierd problem

Thanks.
Have you tried debugging the code by adding printlns to show the values of the variables that control what is happening? Print them out every time they change and when they are used to decide when the ball hits the curtain.

Also the code has too many hardcoded/magic numbers: 900, 680, etc.

These should all be changed to use one or two variables that define the width and height of the box etc.
For testing I wanted to reduce the size of the box for quicker bounces but am having a hard time because if I reduce the size of the frame, it doesn't change the hardcoded values you have in the code.
As it currently is coded, it is NOT possible to make changes for easy debugging.

5. ## Re: bricks wierd problem

```public void check(Brick[][] b) {
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b.length; j++) {
if (b[i][j] != null) {
for (int k = 0; k <= b[i][j].getWidth(); k++) {
if (contains(b[i][j].getX() + k, b[i][j].getY())```
Arrrrrrrrrrgggg!

Kill the ugly code, kill it now.

6. ## Re: bricks wierd problem

i changed it. you shouldn't have a poblem editing now. both width and height are defined in the constructor.

7. ## Re: bricks wierd problem

Originally Posted by Junky
Arrrrrrrrrrgggg!

Kill the ugly code, kill it now.
what other options do i have?

8. ## Re: bricks wierd problem

the ball only hits bricks above a curtain line.
Where is this line that divides where a brick is hit?
Does it relate to the array that holds the bricks?

9. ## Re: bricks wierd problem

Originally Posted by Norm
Where is this line that divides where a brick is hit?
Does it relate to the array that holds the bricks?
what i ment was for some reason the ball hits the bricks on the upper rows of bricks but goes through the lower ones.

10. ## Re: bricks wierd problem

the ball hits the bricks on the upper rows of bricks but goes through the lower ones.
Which rows? Then look at the array that holds the bricks and see if there is a correlation.

11. ## Re: bricks wierd problem

Originally Posted by Norm
Which rows? Then look at the array that holds the bricks and see if there is a correlation.
the ball doesn't hit the 3 bottom rows.
what do you mean by correlation?

12. ## Re: bricks wierd problem

What are the dimensions of the brick array? Is there a relationship of 3 in the dimensions?
How do your loops go thru the brick array looking for collisions?