• Post Reply Bookmark Topic Watch Topic
  • New Topic

JFrame hangs on actionEvent and never returns... Program : Bouncing Ball  RSS feed

 
Funkaj Kohli
Greenhorn
Posts: 12
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !

The spec is to make a bouncing ball which can be stopped and started on a button click.

What I have done :
I have a JPanel(in which the ball bounces) inside a JFrame. Another JPanel has two JButtons "start and stop".
By default the ball is bouncing. On clicking stop, the ball stops (genius right !), however when i press "start", i can see the code running in my console(thanks to good ol' println) but the frame just hangs and never returns... nothing works from there on.

Also, I have tried using random colors with "gradients" but it doesn't seem to be working.

Please help.

Regards,
Pankaj Kohli

 
Funkaj Kohli
Greenhorn
Posts: 12
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//The class which sets the ball rolling
 
Funkaj Kohli
Greenhorn
Posts: 12
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//The class used to set the default operations of a frame
 
Rob Spoor
Sheriff
Posts: 21050
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The go() method is completely executed on the EDT. That method can only end if "flag" is set to false, which can only be done from the EDT. But because go() blocks the EDT that will never happen, and you've effectively blocked the EDT permanently. I suggest you read Concurrency in Swing.

I'd switch to a javax.swing.Timer. That will execute entirely on the EDT, so no synchronization problems. It will also handle its own sleeping etc. The only big change will be "flag". You won't need that anymore to control the loop. Starting means start the timer, stopping means stop the timer.

Oh, and Thread.sleep(1) does not cause a one second sleep - it will cause a one millisecond sleep. Changing this to Thread.sleep(1000) would not solve your hanging GUI problem, but it would decrease the CPU usage of your PC.


I'll move this thread to our user interface forum.
 
Funkaj Kohli
Greenhorn
Posts: 12
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob !!!

Rob Spoor wrote:The go() method is completely executed on the EDT.... I suggest you read Concurrency in Swing.


Thanks for enlightening me.
I can see that my approach is wrong... I had no idea about the EDT. Timer sounds good enough for me

Rob Spoor wrote:
Oh, and Thread.sleep(1) does not cause a one second sleep - it will cause a one millisecond sleep....


You are absolutely right, however, I never suggested this. Longer delay(300 or so) is a little slow for what i want. Thanks for the suggestion nonetheless.

Rob, one last thing, the randomly generated gradient color doesn't seem to be working either...? The ball is always black in color.

Thanks in advance.

Regards,
Pankaj Kohli
 
Rob Spoor
Sheriff
Posts: 21050
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Funkaj Kohli wrote:Thanks Rob !!!

You're welcome.

Rob Spoor wrote:
Oh, and Thread.sleep(1) does not cause a one second sleep - it will cause a one millisecond sleep....


You are absolutely right, however, I never suggested this.

Funkaj Kohli wrote:



Rob, one last thing, the randomly generated gradient color doesn't seem to be working either...? The ball is always black in color.

No idea about that, but I'm sure someone with more painting experience (like Darryl or Rob Camick) will no doubt help you out with that.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Funkaj Kohli wrote:Rob, one last thing, the randomly generated gradient color doesn't seem to be working either...? The ball is always black in color.


Try changing this:
To this:
It's to do with precedence: casting comes before multiplication. And cast a double less than 1 to an int, and you'll get 0.
 
Rob Spoor
Sheriff
Posts: 21050
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D'oh! I can't believe I missed that. The result would of course be that red, green and blue are 0 for both colors and bx, by, ex and ey would are 10.
 
Funkaj Kohli
Greenhorn
Posts: 12
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew. Its working now.... though its not what i thought it'd work like :p


Rob, once again, thanks mate !
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!