Results 1 to 2 of 2
  1. #1
    ferdzz is offline Member
    Join Date
    Jun 2010
    Rep Power

    Default Flipping the direction on the bouncing ball problem!

    Hi all, I'm currently trying to complete the famous basic program "Bouncing Ball". For those who are not familiar with this problem. Here is the spec:

    - write a swing component that extends JPanel
    - contains a ball with x and y coords and with vx, vy random velocities
    - constructor has a thread that updates the position of the ball
    - if the ball hits any of the edges flip the sign of the velocity accordingly
    - paintComponent that draws the ball at its location

    I have completed most of my code but cannot seem to figure out how to make it bounce of the "walls"... The ball seems to get stuck at the top right corner and it just continuously bounces off of the corner.

    Here is my code:

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

    public class BouncingBall extends JPanel
    private double x,y;
    private double vx,vy;
    private static final Random rng = new Random();
    private volatile boolean running = true;

    public BouncingBall(){
    this.setPreferredSize(new Dimension(300,300));
    Random rng = new Random();
    x = 135;
    y = 135;

    vx = rng.nextInt(3)+1;
    vy = rng.nextInt(3)+1;

    new Thread(new Runnable() {
    public void run() {
    while(running) {
    try { Thread.sleep(40); } catch(InterruptedException e) { }


    public void draw(){
    x -= vx;
    y -= vy;
    if (y>300 || y<0){
    y = -y;

    if (x>300 || x<0){
    x = -x;

    public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
    g2.fill(new Ellipse2D.Double(x, y,30,30));

    public static void main (String[] args){
    JFrame f = new JFrame("Bouncing Ball");
    f.add(new BouncingBall());
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE );

    Any help would be appreciated. Thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    Please do not double post your questions as it is considered rude, and for good reason as you then risk splitting the discussion unnecessarily leading to duplication of effort. I'm locking this thread.

Similar Threads

  1. Replies: 4
    Last Post: 06-17-2010, 04:23 AM
  2. Bouncing ball program... having problems..
    By ilovenayoon in forum Java Applets
    Replies: 1
    Last Post: 12-08-2009, 04:16 PM
  3. high bouncing ball
    By bouncingball in forum Reviews / Advertising
    Replies: 1
    Last Post: 06-19-2008, 11:21 AM
  4. bouncing ball issue
    By adam405 in forum New To Java
    Replies: 1
    Last Post: 03-18-2008, 03:48 AM
  5. Problem deleting ball from bouncing ball app
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 09:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts