• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Problem in doing animation using multithreading

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !

Here is a simple piece of code that I have written to move a ball diagonally across the screen using a thread :



I want to extend the idea to multiple threads i.e., multiple balls moving across the screen with their starting points generated randomly. However, I am lost as to how to go about doing the same. For instance, what should the condition be in the while loop in the run method ?
 
Rancher
Posts: 43015
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless the balls do more complicated things, I think representing each with its own thread is overkill. I'd suggest to create a Ball class that holds all required information about one ball (x and y coordinates, speed, direction, color, etc.), and then to have an array of those that the main thread iterates through and manipulates.
 
trish mo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Unless the balls do more complicated things, I think representing each with its own thread is overkill.



The code that I have posted is just a humble starter's attempt at creating an animation with multiple threads, I intend to work my way up with somewhat complex animation...

Here is another excuse for a code that I have written for an animation involving multiple threads.



I fail to understand the output i.e., why don't I see 2 separate balls moving ? Why is there only 1 ball visible at any given time ?
 
Sheriff
Posts: 21922
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you're only drawing one oval each time. But I see quite a few other problems in this code:
- calling repaint on a thread other than the EDT.
- forgetting the call to super.paint(g) at the start of the paint method.
- trying to stop by nulling out the variables; the threads will still continue though.

I suggest you check out javax.swing.Timer. This will trigger events regularly on the EDT itself. If this interval is small enough users will see a fluent movement.
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you intend to use multiple threads, I'd wrap them inside a an inner class that extends Thread with it's own constructor.
 
trish mo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your suggestions, folks...but I am still a beginner so got to dig into your ideas & explore them a bit...will be back with updated results & doubts (hopefully soon).
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Unnar Björnsson wrote:If you intend to use multiple threads, I'd wrap them inside a an inner class that extends Thread with it's own constructor.


Why would you extend Thread? I would, just as he is doing now, use the Runnable interface. Because of multiple reasons.
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic