0

I'm trying to make this Bouncing Ball program have the ball bounce around in the window, but no matter what I do, it bounces once and then just goes indefinitely out of the screen. What do I do to make it stay in the screen?

/*
* File: BouncingBall.java
* This program graphically simulates a bouncing ball. 
*
*
*/
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.Timer;

public class BouncingBall
{
   public static void main()
   {
      JFrame frame = new JFrame( "Bouncing Ball" );
      frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

      BallPanel bp = new BallPanel(); 
      frame.add( bp );
      frame.setSize( 600, 300 ); // set frame size
      frame.setVisible( true ); // display frame
   } // end main
}


// class BallPanel
class BallPanel extends JPanel implements ActionListener
{
   private int delay = 15;
   protected Timer timer;

   private int x = 0;            // x position
   private int y = 0;            // y position
   private int diameter = 20;   // ball diameter

   private int velX = 2;         // velocity offset for x
   private int velY = 2;         // velocity offset for y

   public BallPanel()
   {
      timer = new Timer(delay, this);
      timer.start();        // start the timer
   }

   public void actionPerformed(ActionEvent e)  // This method runs when timer done
   {
    repaint();                // repaint panel to make ball in different place
   }

   public void paintComponent( Graphics g )    // determine ball position
   {
      super.paintComponent( g );    // call superclass's paintComponent 
      g.setColor(Color.black);      // set ball to black

       if (y > getHeight())        // make ball bounce off floor
       {
           velY = -velY;
       }
       x += velX;                  // add velocity offset for new position
       y += velY;                  // add velocity offset for new position
       g.fillOval(x, y, diameter, diameter);
   }

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Possible duplicate of [this](http://stackoverflow.com/q/9849950/230513) or [this](http://stackoverflow.com/q/9800968/230513). – trashgod Apr 14 '16 at 01:34
  • See [Collision detection with complex shapes](http://stackoverflow.com/a/14575043/418556) for a working example. – Andrew Thompson Apr 14 '16 at 03:52
  • Don't check or modify the state in the `paintComponent`, this method could be called for different reasons which you don't initiate, update them within the `actionPerformed` method – MadProgrammer Apr 14 '16 at 06:19
  • In case you'd like something nice and complex, check out this [example](http://stackoverflow.com/questions/13261767/java-ball-object-doesnt-bounce-off-of-drawn-rectangles-like-its-supposed-to/13263022#13263022), bouncing off multiple objects – MadProgrammer Apr 14 '16 at 06:21
  • And in case one ball wasn't enough [multiple balls bouncing](http://stackoverflow.com/questions/13022754/java-bouncing-ball/13022788#13022788) – MadProgrammer Apr 14 '16 at 06:21

2 Answers2

0

You only detect that the ball has hit the floor. You need to also check if the ball has hit the ceiling;

if (y < 0)        // make ball bounce off ceiling
   {
       velY = 2;
   }

Similarly you will need to check for it hitting the left and right sides...

Paul MacGuiheen
  • 628
  • 5
  • 17
0

You only check the Y-Coordinate and only that if is below 0.

if(y <= 0 || y >= 300 || x <= 0 || x >= 600)

Replace that with your if-statement and it should work.