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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: bricks wierd problem

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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);
    				}
    			}
    		}
     
    		Thread t = new Thread(new Runnable() {
     
    			@Override
    			public void run() {
    				while (true) {
    					repaint();
    					ball.check(bricks);
    					try {
    						Thread.sleep(10);
    					} 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);
    		f.add(new bounce());
    	}
     
    }
    Last edited by bardd; August 8th, 2011 at 07:18 AM.
    a good programmer can write a Java program in any language


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,562
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

    Default 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.
    Last edited by Norm; August 6th, 2011 at 07:19 AM.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: bricks wierd problem

    i forgot the imports. it should work now.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,562
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

    Default 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.
    Last edited by Norm; August 6th, 2011 at 10:43 AM. Reason: Notes on magic, hardcoded numbers.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default 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.
    Improving the world one idiot at a time!

  6. #6
    Junior Member
    Join Date
    Sep 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: bricks wierd problem

    i changed it. you shouldn't have a poblem editing now. both width and height are defined in the constructor.
    a good programmer can write a Java program in any language

  7. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: bricks wierd problem

    Quote Originally Posted by Junky View Post
    Arrrrrrrrrrgggg!

    Kill the ugly code, kill it now.
    what other options do i have?
    a good programmer can write a Java program in any language

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,562
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

    Default 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. #9
    Junior Member
    Join Date
    Sep 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: bricks wierd problem

    Quote Originally Posted by Norm View Post
    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.
    a good programmer can write a Java program in any language

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,562
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

    Default 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. #11
    Junior Member
    Join Date
    Sep 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: bricks wierd problem

    Quote Originally Posted by Norm View Post
    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?
    a good programmer can write a Java program in any language

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,562
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

    Default 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?

Similar Threads

  1. Wierd problem
    By kalle82 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 29th, 2010, 04:30 PM