Hello everyone I am having a hard time bouncing a ball I cant seem to get the collision correct and the ball is moving way to fast.Any help would be greatly appreciated.Here is my code.
import java.awt.*; import javax.swing.*; /** * Created by IntelliJ IDEA. * User: aortell24 * Date: 7/31/12 * Time: 4:41 PM * To change this template use File | Settings | File Templates. */ public class Ball extends JPanel { private Color _color; private int _radius; private int _xSpeed; private int _ySpeed; private int _xPoint; private int _yPoint; private double _direction; public Ball() { _color = Color.YELLOW; _radius = 5; _direction = Math.random() * 360; } public Color getColor() { return _color; } public double getDirection() { return _direction; } public int getXSpeed() { return _xSpeed; } public void setXSpeed(int xSpeed) { _xSpeed = xSpeed; } public int getYSpeed() { return _ySpeed; } public void setYSpeed( int ySpeed) { _ySpeed = ySpeed; } public int getXPoint() { _xPoint = (int)(Math.random() * 800 +5); return _xPoint; } public int getYPoint() { _yPoint = (int)(Math.random() * 800 +5); return _yPoint; } public void setXPoint(int xPoint) { _xPoint = xPoint; } public void setYPoint(int yPoint) { _yPoint = yPoint; } public void setColor(Color color) { _color = color; } public void setDirection(double direction) { _direction = direction; } public void setRadius(int radius) { _radius = radius; } public int getRadius() { return _radius; } protected void paintComponent(Graphics graphics) { graphics.setColor(getColor()); graphics.fillOval(0,0, getRadius() * 2, getRadius() * 2); graphics.setColor(Color.BLACK); graphics.drawOval(0,0,getRadius() *2, getRadius() * 2); } } import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * Created by IntelliJ IDEA. * User: aortell24 * Date: 7/31/12 * Time: 6:34 PM * To change this template use File | Settings | File Templates. */ public class Board extends JFrame { JPanel panel1 = new JPanel(); Ball ball = new Ball(); Timer timer = new Timer(10,new TimerEventHandler()); int xPoint =0; int yPoint =0; Board() { timer.start(); this.setLayout(null); panel1.setLayout(null); panel1.setBounds(0,0,800,800); this.add(panel1); panel1.add(ball); panel1.setBackground(Color.BLACK); if(ballHitsLeftOrRight() || ballHitsTopOrBottom()) { ballBounce(); } } public boolean ballHitsTopOrBottom() { if(ball.getY() >= 800) return true; else if(ball.getY() <= 0) return true; else return false; } public boolean ballHitsLeftOrRight() { if(ball.getX() >= 800) return true; else if(ball.getY() <= 0) return true; else return false; } public void ballBounce() { if(ballHitsLeftOrRight()) { ball.setXSpeed(-xPoint); } if(ballHitsTopOrBottom()) { ball.setYSpeed(-yPoint); } } class TimerEventHandler implements ActionListener { public void actionPerformed(ActionEvent actionEvent) { ball.setBounds(ball.getXPoint() + ball.getXSpeed(),ball.getYPoint() + ball.getYSpeed(),15,15); repaint(); } } } import javax.swing.*; /** * Created by IntelliJ IDEA. * User: aortell24 * Date: 7/31/12 * Time: 6:42 PM * To change this template use File | Settings | File Templates. */ public class Application { public static void main(String[] args) { Board board = new Board(); board.setSize(800, 600); board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); board.setLocationRelativeTo(null); board.setVisible(true); } }