473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Bouncing Ball Just Suddenly Stops Mid Bounce

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
2 1752
I was able to resolve this one. Thanks
Dec 3 '07 #2
I was able to resolve this one. Thanks

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

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

Similar topics

2
by: steve | last post by:
I'm trying to create a PHP script to send mail to a list of addresses. I'm sending mail okay, because the messages arrive in an account that I can check, but messages to a known bad address aren't...
2
by: Shaun | last post by:
Hi Odd one here, I have a local version of SQL running on my laptop for demos, been working fine for years yet yesterday I suddenly get an error "Invalid Conection Parameter" running a sproc...
2
by: Nils Hedström | last post by:
Today my stateserver (running at the same computer at the web-server) suddenly stopped responding (after 10 days working perfectly). I did not see any entry in the eventlog indicating that the...
12
by: Kristoffer Arfvidson | last post by:
Hello, does anybody have a bouncing prograssbar control? Like in xp, you have a normal progressbar but instead of going from left to right and fill out the whole progressbar and then stop. I...
0
by: vardium | last post by:
Hello, I have a problem on VS 2005 asp.net development server. When I am trying to debug the application, I attach to the "WebDev.WebServer.EXE" process. There is no problem until here. I can...
6
by: Pieter | last post by:
Hi, I developed an Outlook Add In (VB.NET 2005, for Outlook 2003) that sometimes suddenly stops loading. I've put everywhere in the code Exception Handlers, but no Exception is catched. it...
3
by: Niron kag | last post by:
Hi, I wrote a windows service in c# in .NET 1.1 and SQL Stored Procedures, and I run it on Microsoft windows server 2003 service pack 1. The service works every 10 minutes and pass data between...
1
ilikepython
by: ilikepython | last post by:
Hi, I made a pong game with pygame. It works pretty good but I want the ball to bounce realistically. Just like in real pong when it bounces off the right edge it goes to the right and when it...
18
by: Grant Edwards | last post by:
Could whoever is responsible for the gateway that is grabbing my postings off of Usenet and e-mailing them out please fix the headers in the mail messages so that I don't get the bounce messages?...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.