Urgent Question: Bouncing Balls in a JPanel (Java 2)

Posted on 2003-12-09
Last Modified: 2010-05-18
this is for an assignment in which I extend a JPanel into a "BouncePanel" and then bounce a couple of balls all over the JPanel. When I run the program, the ball just moves across to the right and disappears from view. I believe that my problem is in how I am writing my if...then statements. I have been over this and over it and I can't figure out how to make multiple balls bounce around.
Thanks much,

Here is a portion of the assignment that I think is most relevant:
3. Also inside Bouncer, define a move() method that will be called every time the timer ticks.
It should:
a. Add xspeed to x, thus moving the ball to a new x-position.
b. If x < 0 the ball is beyond the left edge. Bring it back to the edge by setting x=0, and
reverse the direction of motion by setting xspeed = -xspeed.
c. If x > xmax then the ball is beyond the right edge. Bring it back to the edge by setting
x=xmax and reverse direction by setting xspeed = -xspeed.
d. Repeat steps a,b,c but use y, yspeed, and ymax.
e. Move the ball by calling setCenter() and sending the new x and y values.
4. In the main program, after you do the setVisible(), create a Timer object with a delay of
20 milliseconds (=1/50 second) and tell it to use the bp as its ActionListener. Then start the
Timer t = new Timer(20,bp);
5. Modify the class BouncePanel so it implements ActionListener. In the actionPerformed()
a. Determine the current dimensions of the BouncePanel and store them into the xmax and
ymax variables of class Bouncer. For example
Bouncer.xmax = this.getSize().width;
b. Obtain the array of Components that have been added to this BouncePanel
Component[] ca = this.getComponents();
c. Loop through the array and tell Java to verify that each Component is a Bouncer and then
call the Bouncer's move() method.
d. After moving all the balls, it may be necessary to call bp.repaint() to get them to show.

Java Code is as follows:
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.*;

class BouncePanel1 extends JPanel implements ActionListener {
      BouncePanel1() {
            this.setPreferredSize(new Dimension(600,600));

public static void main(String args[]) {
            JFrame jf1 = new JFrame();
            JPanel cp = new JPanel();
            BouncePanel1 bounce1 = new BouncePanel1();
            Bouncer ball1 = new Bouncer(250, Color.YELLOW);
            Bouncer ball2 = new Bouncer(250, Color.CYAN);
            Timer t = new Timer(20, bounce1);

      } // end method main

      public void actionPerformed(ActionEvent ae) {
                  Bouncer.xmax = this.getSize().width;
                  Component[] ca = this.getComponents();
                  for(int i=0; i<ca.length; i++) {

      }      // end ActionPerformed method

}      // end class BouncePanel

class Bouncer extends JComponent {
      static int xmax, ymax;
      double x, y, xspeed, yspeed;
      int r = 10;
      Bouncer(int r, Color c) {
            this.setSize(2*r, 2*r);

      }      // end Bouncer constructor

      public void setCenter(double x, double y) {
            setLocation((int)x-r, (int)y-r);

      }      // end setCenter method

      public void move() {
            xspeed = 5;
            yspeed = 5;
            x = xspeed + x;
            y = yspeed + y;

            if (x<0) {
                  x = 0;
                  xspeed = -xspeed;
            } else if (x > xmax) {
                  x = xmax;
                  xspeed = -xspeed;

            if (y < 0) {
                  y = 0;
                  yspeed = -yspeed;
                  } else if  (y > ymax) {
                        y = ymax;
                        yspeed = -yspeed;


      }      // end move method

      public void paint( Graphics g ) {
            g.fillOval(0, 0, 2*r, 2*r);
            g.drawString( "Welcome to", 25, 25 );
            g.drawString( "December", 25, 40 );

      } // end paint method

}             // end class Bouncer

Question by:archeron1
  • 3
LVL 92

Accepted Solution

objects earned 500 total points
Comment Utility
>         xspeed = 5;
>         yspeed = 5;

Don't set speed everytime move() is called. Just initialise it once in your ctor.

LVL 92

Expert Comment

Comment Utility
unrelated, but you should also override paintComponent() instead of paint() (same args, you just need to change method name).
LVL 92

Expert Comment

Comment Utility

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now