By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,542 Members | 1,740 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,542 IT Pros & Developers. It's quick & easy.

Bouncing Ball Just Suddenly Stops Mid Bounce

P: 4
I have a application where each time you click the add ball button a new randomly colored ball is added to the jpanel. It works fine except that the balls suddenly stop at the same spot. I want them to continue to bounce for a longer period of time instead of like 8 bounces. It was going around withoutstopping prior to my getting the add new ball part working. I cannot seem to figure out what I did wrong. Also curious if anyone new of a tip as to how to get the balls to disappear once a button is clicked or their cycle runs out. Below is my code:

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.geom.*;
  4. import java.util.*;
  5. import javax.swing.*;
  6.  
  7. public class BounceBall{
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.         JFrame frame = new BounceFrame();
  12.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13.         frame.setVisible(true);
  14.     }
  15. }
  16.  
  17. class BounceFrame extends JFrame {
  18.  
  19.     private BallCanvas canvas;
  20.     public static final int WIDTH = 450;
  21.     public static final int HEIGHT = 350;
  22.  
  23.  
  24.     public BounceFrame() {
  25.         setSize(WIDTH, HEIGHT);
  26.         setTitle("BounceThread");
  27.  
  28.         Container contentPane = getContentPane();
  29.         canvas = new BallCanvas();
  30.         contentPane.add(canvas, BorderLayout.CENTER);
  31.         JPanel buttonPanel = new JPanel();
  32.         addButton(buttonPanel, "Add Ball",
  33.                 new ActionListener() {
  34.             public void actionPerformed(ActionEvent evt) {
  35.                 addBall();
  36.             }
  37.         });
  38.  
  39.         addButton(buttonPanel, "Exit",
  40.                 new ActionListener() {
  41.             public void actionPerformed(ActionEvent evt) {
  42.                 System.exit(0);
  43.             }
  44.  
  45. class BallThread extends Thread {
  46.  
  47.     private BouncingBall b;
  48.  
  49.     public BallThread(BouncingBall aBall) { b = aBall; }
  50.  
  51.     public void run() {
  52.         try {
  53.             for (int i = 1; i <= 1000; i++) {
  54.                 b.move();
  55.                 sleep(5);
  56.             }
  57.         } catch (InterruptedException exception) {
  58.         }
  59.     }
  60.  
  61. }
  62.  
  63. class BallCanvas extends JPanel {
  64.  
  65.     private ArrayList balls = new ArrayList();
  66.  
  67.     public void add(BouncingBall b) {
  68.         balls.add(b);
  69.     }
  70.  
  71.     public void paintComponent(Graphics g) {
  72.         super.paintComponent(g);
  73.         Graphics2D g2 = (Graphics2D)g;
  74.         for (int i = 0; i < balls.size(); i++) {
  75.             BouncingBall b = (BouncingBall)balls.get(i);
  76.             b.draw(g2);
  77.         }
  78.     }
  79. }
  80.  
  81. class BouncingBall {
  82.  
  83.     private Component canvas;
  84.     private static final int XSIZE = 15;
  85.     private static final int YSIZE = 15;
  86.     private int x = 0;
  87.     private int y = 0;
  88.     private int dx = 2;
  89.     private int dy = 2;
  90.  
  91.     public BouncingBall(Component c) { canvas = c; }
  92.  
  93.     Color color = getColor();
  94.  
  95.     public void draw(Graphics2D g2) {
  96.  
  97.         g2.setColor(color); 
  98.         g2.fillOval(x,y,30,30);   // adds color to circle
  99.         g2.drawOval(x,y,30,30);   // draws circle
  100.  
  101.     }
  102.  
  103.     public void move() {
  104.         x += dx;
  105.         y += dy;
  106.         if (x < 0) {
  107.             x = 0;
  108.             dx = -dx;
  109.         }
  110.         if (x + XSIZE >= canvas.getWidth()) {
  111.             x = canvas.getWidth() - XSIZE;
  112.             dx = -dx;
  113.         }
  114.         if (y < 0) {
  115.             y = 0;
  116.             dy = -dy;
  117.         }
  118.         if (y + YSIZE >= canvas.getHeight()) {
  119.             y = canvas.getHeight() - YSIZE;
  120.             dy = -dy;
  121.         }
  122.  
  123.         canvas.repaint();
  124.     }
  125.  
  126.     private Color getColor() {
  127.         int rval = (int)Math.floor(Math.random() * 256);
  128.         int gval = (int)Math.floor(Math.random() * 256);
  129.         int bval = (int)Math.floor(Math.random() * 256);
  130.         return new Color(rval, gval, bval);
  131.    }
  132.  
  133.     }
  134.  
Dec 2 '07 #1
Share this Question
Share on Google+
2 Replies


P: 4
I was able to resolve this one. Thanks
Dec 3 '07 #2

P: 54
I was able to resolve this one. Thanks

can u post all your code for reference ?
Dec 4 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.