• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

New to Java. Need some proformance suggestions

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to java. I am teaching myself the language. I have this bouncing ball program. When the start button is pressed the ball drops and bounces around the panel. Each time you press start a new ball is created. The problem is each new ball slows down the program. Is there a way to correct this?

Sorry if this isn't the correct place for this topic.




[ December 22, 2008: Message edited by: Lamr Jared ]
 
Marshal
Posts: 77185
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

How are you scheduling re-drawing the Balls? You are using Thread.sleep(). I think Thread.sleep(1) is a bit short; it can take more than 1 millisecond to repaint the entire GUI. Try giving a longer delay, maybe 20.

There have been problems on Windows with timings which don't divide exactly by 10. I can't remember the details, but it can cause the system clock to show incorrect times.

That sort of question is more difficult than the average beginner's question; it might be appropriate for the Swing forum or the Performance forum, but I think you might get more attention in Swing . . . moving.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I think you have several problems with this, but the biggest is that you have every ball have its own thread.The more balls you have (quiet down kiddies ) the more threads, and the more work it is to schedule between the threads.

The better bet would be to have just a few background threads, 1 to schedule the moving and another 1 to schedule repainting. Both should be scheduled at a fixed rate, rather than using Thread.sleep, which would be a fixed delay. Take a look at Timer and TimerTask, or ScheduledExecutorService to do this.

The Ball itself would be a normal object which keeps track of its position and can handle painting itself.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> So I think you have several problems with this,...

there are major problems with the whole lot:
- indiscremenent use of the swing thread
- getGraphics() (which is a wrist-slasher)

the only hope is ctrl-a-delete and start again.

to start again, start here:

http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html
 
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lamr, please Use A Meaningful Subject Line. "Java Help" doesn't quite describe the problem accurately; about 99% of all threads are about Java help.

You can use the edit button to change it.
 
Lamar Jared
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I appreciate all the help. I am only 6 months in working with Java. I don't know all the strengths yet. Thanks again everyone.
[ December 22, 2008: Message edited by: Lamar Jared ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic