Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-03-2007, 06:14 AM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
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 without stopping 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:

Code:
import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.util.*; import javax.swing.*; public class BounceBall{ public static void main(String[] args) { JFrame frame = new BounceFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E); frame.setVisible(true); } } class BounceFrame extends JFrame { private BallCanvas canvas; public static final int WIDTH = 450; public static final int HEIGHT = 350; public BounceFrame() { setSize(WIDTH, HEIGHT); setTitle("BounceThread"); Container contentPane = getContentPane(); canvas = new BallCanvas(); contentPane.add(canvas, BorderLayout.CENTER); JPanel buttonPanel = new JPanel(); addButton(buttonPanel, "Add Ball", new ActionListener() { public void actionPerformed(ActionEvent evt) { addBall(); } }); addButton(buttonPanel, "Exit", new ActionListener() { public void actionPerformed(ActionEvent evt) { System.exit(0); } class BallThread extends Thread { private BouncingBall b; public BallThread(BouncingBall aBall) { b = aBall; } public void run() { try { for (int i = 1; i <= 1000; i++) { b.move(); sleep(5); } } catch (InterruptedException exception) { } } } class BallCanvas extends JPanel { private ArrayList balls = new ArrayList(); public void add(BouncingBall b) { balls.add(b); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; for (int i = 0; i < balls.size(); i++) { BouncingBall b = (BouncingBall)balls.get(i); b.draw(g2); } } } class BouncingBall { private Component canvas; private static final int XSIZE = 15; private static final int YSIZE = 15; private int x = 0; private int y = 0; private int dx = 2; private int dy = 2; public BouncingBall(Component c) { canvas = c; } Color color = getColor(); public void draw(Graphics2D g2) { g2.setColor(color); g2.fillOval(x,y,30,30); // adds color to circle g2.drawOval(x,y,30,30); // draws circle } public void move() { x += dx; y += dy; if (x < 0) { x = 0; dx = -dx; } if (x + XSIZE >= canvas.getWidth()) { x = canvas.getWidth() - XSIZE; dx = -dx; } if (y < 0) { y = 0; dy = -dy; } if (y + YSIZE >= canvas.getHeight()) { y = canvas.getHeight() - YSIZE; dy = -dy; } canvas.repaint(); } private Color getColor() { int rval = (int)Math.floor(Math.random() * 256); int gval = (int)Math.floor(Math.random() * 256); int bval = (int)Math.floor(Math.random() * 256); return new Color(rval, gval, bval); } }
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-03-2007, 03:58 PM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
I've resolved this one so no need to respond Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple bouncing balls Algar AWT / Swing 2 04-24-2008 09:35 PM
bouncing ball issue adam405 New To Java 1 03-18-2008 04:48 AM
How do I make My ball to move randomly? whdbstjr90 New To Java 4 12-31-2007 06:32 PM
Problem deleting ball from bouncing ball app adlb1300 New To Java 2 12-03-2007 10:08 PM
Need help making ball move and bounce off of sides of JPanel adlb1300 New To Java 2 12-01-2007 08:48 AM


All times are GMT +3. The time now is 05:47 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org