I'm creating a bouncing ball project , the balls are generated with random velocity , random size and random positioning each time the mouse is clicked , everything is working perfect except for one thing , the balls when they are hitting let's say the walls from the top and left it's perfectly getting bounced back but when the ball hits the bottom and right portions of the window they are getting bounced too but not perfectly , what I mean is that the balls bounce back from the right of the window before even hitting the wall and from the bottom when almost half the ball exceeds the window. What is the problem, why it's doing that ?
Here is the Code :
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
//To generate random colors
/*new Color((int) Math.floor((Math.random() * 256)),(int) Math.floor((Math.random() * 256)),(int)
Math.floor((Math.random() * 256)))*/
public class bouncingBalls {
public static void main(String[] args) {
Program program = new Program();
program.run();
}
}
class Program {
protected JFrame mainFrame;
protected DrawPanel drawPanel;
protected java.util.List<Ball> balls;
void run() {
balls = new ArrayList<>();
mainFrame = new JFrame();
drawPanel = new DrawPanel();
mainFrame.getContentPane().add(drawPanel);
mainFrame.setTitle("Bouncing Balls");
mainFrame.setSize(640, 480);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* Generate balls */
mainFrame.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
System.out.println("Mouse clicked");
Ball ball = new Ball(
/* Random positions from 0 to windowWidth or windowHeight */
(int) Math.floor(Math.random() * 640),
(int) Math.floor(Math.random() * 480),
/* Random size from 10 to 30 */
(int) Math.floor(Math.random() * 20) + 10,
/* Random RGB colors*/
new Color(0x851E3E),
/* Random velocities from -5 to 5 */
(int) Math.floor((Math.random() * 10) - 5),
(int) Math.floor((Math.random() * 10) - 5)
);
balls.add(ball);
}
});
while (true) {
for (Ball b: balls) {
b.update();
}
/* Give Swing 10 milliseconds to see the update! */
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
mainFrame.repaint();
}
}
class DrawPanel extends JPanel {
@Override
public void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
for (Ball b: balls) {
b.draw(graphics);
}
}
}
class Ball {
private int posX, posY, size;
private Color color;
private int vx;
private int vy;
public Ball(int posX, int posY, int size, Color color, int vx, int vy) {
this.posX = posX;
this.posY = posY;
this.size = size;
this.color = color;
this.vx = vx;
this.vy = vy;
}
void update() {
if (posX > mainFrame.getWidth()-size*2 || posX < 0) {
vx *= -1;
}
if (posY > mainFrame.getHeight()-size-32 || posY < 0) {
vy *= -1;
}
if (posX > mainFrame.getWidth()-size*2) {
posX = mainFrame.getWidth()-size*2;
}
if (posX < 0) {
posX = 0;
}
if (posY > mainFrame.getHeight()-size-32) {
posY = mainFrame.getHeight()-size-32;
}
if (posY < 0) {
posY = 0;
}
this.posX += vx;
this.posY += vy;
}
void draw(Graphics g) {
g.setColor(color);
g.fillOval(posX, posY, size, size);
g.getClipBounds();
g.getClipBounds();
}
}
}