This is my ball class in java...........

public class Ball {
public int width = 30;
public int height = 30;
public int motionX;
public int motionY;
public int x, y;
private RectAnimation pong;
public Random random;
public int amountOfHits;
public Ball(RectAnimation pong) {
this.random = new Random();
this.pong = pong;
spawn();
}
public void update(paddle paddle1, paddle paddle2) {
int speed = 5;
this.x += motionX * speed;
this.y += motionY * speed;
if (this.y + height > pong.height || this.y < 0) {
if (this.motionY < 0) {
this.y = 0;
this.motionY = random.nextInt(4);
if (motionY == 0) {
motionY = 1;
}
} else {
this.y = pong.height - height - motionY;
this.motionY = -random.nextInt(4);
if (motionY == 0) {
motionY = -1;
}
}
}
if (checkCollision(paddle1) == 1) {
this.motionX = 1 + (amountOfHits / 5);
this.motionY = -2 + random.nextInt(4);
if (motionY == 0) {
motionY = 1;
}
amountOfHits++;
} else if (checkCollision(paddle2) == 1) {
this.motionX = -1 - (amountOfHits / 5);
this.motionY = -2 + random.nextInt(4);
if (motionY == 0) {
motionY = -1;
}
}
if (checkCollision(paddle1) == 2) {
paddle2.score++;
spawn();
} else if (checkCollision(paddle2) == 2) {
paddle1.score++;
spawn();
}
}
public void spawn() {
this.amountOfHits = 0;
this.x = pong.width / 2 - this.width / 2;
this.y = pong.height / 2 - this.height / 2;
this.motionX = -1 + random.nextInt(4);
if (motionY == 0) {
motionY = 1;
}
if (random.nextBoolean()) {
motionX = 1;
} else {
motionX = -1;
}
}
public int checkCollision(paddle pad) {
if (this.x < pad.x + pad.width && this.x + width > pad.x && this.y < pad.y + pad.height && this.y + height > pad.y) {
return 1; //bounce
} else if (pad.x > x + width && pad.paddleNumber == 1 || pad.x < x && pad.paddleNumber == 2) {
return 2; //score
}
return 0;
}
public void render(Graphics g) {
g.setColor(Color.WHITE);
g.fillOval(x, y, width, height);
}
}
How do it move you ask and instantiated....
public class RectAnimation implements ActionListener, KeyListener {
public static RectAnimation rectAnimation;
public gamePong renderer;
public paddle player1;
public paddle player2;
public Ball ball;
public int width = 700, height = 700;
public boolean bot = false;
public boolean w = false;
public boolean s = false;
public boolean up = false;
public boolean down = false;
public int gameStatus = 0;
public int botMoves;
public int botCoolDown = 0;
public int botDifficulty = 0;
public RectAnimation() {
Timer timer = new Timer(20, this);
JFrame frame = new JFrame("Rectangle Animation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(true);
frame.setSize(width + 15, height + 30);
frame.setLocation(375, 55);
renderer = new gamePong();
frame.add(renderer);
frame.addKeyListener(this);
timer.start();
}
public void render(Graphics2D g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, width, height);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (gameStatus == 0) {
g.setColor(Color.WHITE);
g.setFont(new Font("Ariel", 1, 50));
g.drawString("PONG", width / 2 - 70, 50);
g.setFont(new Font("Ariel", 1, 20));
g.drawString("Press Space to Play", width / 2 - 90, height / 2 + 50);
}
if (gameStatus == 1 || gameStatus == 2) {
g.setColor(Color.WHITE);
g.setStroke(new BasicStroke(10));
g.drawLine(width / 2, 0, width / 2, height);
g.drawOval(width / 2 - 100, height / 2 - 100, 200, 200);
g.setFont(new Font("Ariel", 1, 50));
g.drawString(String.valueOf(player1.score), width / 2 - 70, 50);
g.setFont(new Font("Ariel", 1, 50));
g.drawString(String.valueOf(player2.score), width / 2 + 65, 50);
player1.render(g);
player2.render(g);
ball.render(g);
}
if (gameStatus == 1) {
g.setColor(Color.WHITE);
g.setFont(new Font("Ariel", 1, 50));
g.drawString("PAUSED", width / 2 - 100, 50);
}
}
public void start() {
player1 = new paddle(this, 1);
player2 = new paddle(this, 2);
ball = new Ball(this);
gameStatus = 2;
}
public void update() {
if (w) {
player1.move(true);
} else if (s) {
player1.move(false);
} else if (!bot) {
if (up) {
player2.move(true);
}
if (down) {
player2.move(false);
}
} else {
if (botCoolDown > 0) {
botCoolDown--;
if (botCoolDown == 0) {
botMoves = 0;
}
}
if (botMoves < 10) {
if (player2.y + player2.height < ball.y) {
player2.move(false);
botMoves++;
}
if (player2.y + player2.height > ball.y) {
player2.move(true);
botMoves++;
}
if(botDifficulty == 0)
botCoolDown = 30;
else if(botDifficulty == 1)
botCoolDown = 20;
else if(botDifficulty == 2)
botCoolDown = 10;
}
}
ball.update(player1, player2);
}
public void actionPerformed(ActionEvent e) {
if (gameStatus == 2) {
update();
}
renderer.repaint();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
rectAnimation = new RectAnimation();
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
int id = e.getKeyCode();
if (id == KeyEvent.VK_W) {
w = true;
} else if (id == KeyEvent.VK_S) {
s = true;
} else if (id == KeyEvent.VK_UP) {
up = true;
} else if (id == KeyEvent.VK_DOWN) {
down = true;
} else if (id == KeyEvent.VK_ESCAPE && gameStatus == 2) {
gameStatus = 0;
} else if (id == KeyEvent.VK_SHIFT && gameStatus == 0) {
bot = true;
start();
}
if (id == KeyEvent.VK_SPACE) {
if (gameStatus == 0) {
start();
bot = false;
} else if (gameStatus == 1) {
gameStatus = 2;
} else if (gameStatus == 2) {
gameStatus = 1;
}
}
}
public void keyReleased(KeyEvent e) {
int id = e.getKeyCode();
if (id == KeyEvent.VK_W) {
w = false;
}
if (id == KeyEvent.VK_S) {
s = false;
}
if (id == KeyEvent.VK_UP) {
up = false;
}
if (id == KeyEvent.VK_DOWN) {
down = false;
}
}
}