Results 1 to 5 of 5
  1. #1
    ferdzz is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

    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));
    this.setBackground(Color.BLACK);
    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) { }
    draw();
    repaint();
    }
    }
    }).start();

    }

    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) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
    RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(Color.WHITE);
    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 );
    f.setVisible(true);
    f.pack();
    }
    }

    Any help would be appreciated. Thanks!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    How does your algorithm control the balls movement?
    If the ball is to move to the right, then you must increment the x value.
    If the ball is to move to the left, then you must decrement the x value.


    Hint: set the increment value to be + when moving to the right and to - when moving to the left.
    x = x + incrVal; // move to next location
    incrVal is +1 to go right and -1 to go left

    How are you changing the values? Use println() statements to print the values after each time you change them.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    ferdzz is offline Member
    Join Date
    Jun 2010
    Posts
    12
    Rep Power
    0

    Default

    My mistake, i just figured i put it under the wrong category

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ferdzz View Post
    My mistake, i just figured i put it under the wrong category
    No problem. But if you request, we can move threads, and in fact I will move this one and leave a re-direct in its old position.

Similar Threads

  1. Bouncing ball program... having problems..
    By ilovenayoon in forum Java Applets
    Replies: 1
    Last Post: 12-08-2009, 04:16 PM
  2. How to create a bouncing ball animation
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 08:43 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
  •