• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Event Problems: Need Help

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This program is not doing what its supposed to do:



What i was hoping is that if i click the bounce button, the ball will move up and down.. but it's not doing it..

Anybody can please explain what's wrong with the program?
 
Marshal
Posts: 80097
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try putting a delay with Thread.sleep(20) between successive calls to move.
The problem is that your calls to move the ball occur very quickly, maybe 0.1microsecond each, and the entire 200 movements are completed before the repaint() call has a chance to do anything.
Remember Thread.sleep() declares a checked InterruptedException.
 
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code you're running is running in the Event Dispatcher Thread. This is also the same thread that will do the painting - but only after an event is completely handled. Therefore, you should never ever but lengthy operations in event handlers.

You'll need to create a new Thread which will do the loop for you, and then wrap the calls to repaint in EventQueue.invokeLater.

Alternatively, you can check out SwingWorker:

How this works:
- worker.execute() creates a new Thread that executes doInBackground.
- publish takes the arguments, puts them in a List, and then makes sure process is called on the Event Dispatch Thread.
- when doInBackground is finished, done() is called on the Event Dispatch Thread again. This can be used to finish things up if needed.

So just call this code in your actionPerformed method, and it should work.


Also, Campbell is right about the sleep time. You have specified 1ms, whereas the human eye can only process around 24 frames per second (I believe). So everything below 40 will be hard to notice. You could still go for something lower but I wouldn't go below 20.
[ August 31, 2008: Message edited by: Rob Prime ]
 
Campbell Ritchie
Marshal
Posts: 80097
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ought to have read your post properly; you already have Thread.sleep in use. Sorry. But 1 millisecond is probably too short a delay. Try 20 or 30 milliseconds instead.
 
Beth Laguardia
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for the reply. I am not familiar yet with swing worker..
But will check it and try to understand..

The loop works when its inside the go() method. Ii was able to bounce the ball. But when i try to put it to the other class, its not working anymore..

Is there any alternative aside from swing worker?

Thank you all for the quick response.. I really appreaciate it..
 
Campbell Ritchie
Marshal
Posts: 80097
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best solution is to use SwingWorker which you can read about in the Java Tutorials. As Rob suggested.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Beth Laguardia:
Thank you all for the reply. I am not familiar yet with swing worker.. Is there any alternative aside from swing worker?


Yep, I'd actually use a Swing Timer here. It's easier to use than a SwingWorker, and since you don't have any resource-hungry blocks, I don't really see the need for a background thread at all (other than the behind-the-scenes background thread provided by Swing Timer).
 
Rob Spoor
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know, I didn't even think of the Swing Timer. Its tasks also have the advantage of running on the Event Dispatcher Thread so you just write an ActionListener that moves the ball, schedule it in the Timer and voila!
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A couple of other recommendations:
1) Get rid of your "magic numbers" such as 300, 40, 400, 70. They will make it very hard to update and debug the program. Use constants and fields where you can.
2) You can get rid of the calls to g.setColor(Color.white); and g.fillRect(...) by simply adding a call to super.paintComponent(g) in your paintComponent method, and a call to drawPanel.setBackground(Color.white); in your "go" method.
3) Rather than have Bounce's methods directly manipulate BouncingBall's fields, it would be far better to create a constructor for Bounce that takes a BouncingBall object as a parameter and have your Bounce class call a public setYPosition(int yPosition) method. In my SSCCE my Bounce constructor's signature looked like this:
 
Campbell Ritchie
Marshal
Posts: 80097
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good idea, Pete
 
Beth Laguardia
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh my! I am fairly new in java so I some of this i have not tackle yet but thanks to the reply..

I guess i will just stop beig mbitious and do not go to graphics yet.. hehe

But thank you all!
 
Campbell Ritchie
Marshal
Posts: 80097
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you ought to find a bouncing ball example which works and study it very carefully. It is a very common exercise, since it leads one into threading. Don't give up graphics, but take it slowly, because there is so much to learn very quickly.
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic