• 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

simplest java animation with repaint thread  RSS feed

Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I was trying to make the simple animation with repaint in a separate thread.
Where did I go wrong?

Posts: 63791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't apply two threads to a Swing application; Swing is to all intents and purposes non‑threa‍d‑safe. Do everything in sight on the Event Dispatch Threa‍d.
I don't think it's a good idea to use Threa‍d#sleep. While the Th‍read is sleeping, there is no chance for anything to be repainted; you may not have enough time before it sleeps to move your animation and do the repainting. I suggest you find out how to use a Swing Timer instead. Try setting the frequency of impulses from the Timer between every 20ms and every 40ms and see what happens.
Don't give the paintComponent() method public access. Make it protected. Also, you are not removing the old image, so you are liable to get a green stripe from top left to bottom right. To avoid that, insert the following as the first line of paintComponent():-Moving this discussion to our GUIs forum, and our Threa‍ds forum.
Posts: 3119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Campbell makes clear, in the few lines of your code, you really make things complicated!

Therefore I will make some additional remarks.

First of all, if you are working with Swing, then make sure that the Swing parts of your code are executed on the EDT Thread. That may sound a little vague, but it will become more clear with increasing knowledge and experience.

Your class NewClass extends JPanel. However, it is nowhere used as you can see.
What happens is that in go() a JFrame is created, with a MyDrawPanel as part of the contentPanel. That frame is being made visible at the end of the go() method, therefore you will see the green oval appearing.

Then you start a Thread, that does a repaint() AND a redraw(). Now, the repaint() invokes the paint() method of the instance of NewClass. That method is not overridden, so it invokes the paint() method of the orginal JPanel class. The only thing that happens is that the instance of NewClass is cleared to the background of it, but since this instance belongs to no container at all, nothing visibly happens.
That leaves the redraw() method. As you can see, in this redraw() nothing happens with respect to the frame and to MyDrawPanel.

I took the liberty to implement Campbells and my remarks in your code as best as I could, while trying to change as little as I can.

There is more to be remarked however, and it gets a bit complicated. If you look at the panel (with the code I give later), you see that the redrawing of the panel takes place on the EDT, as you would expect. Now, the whole instance of NewPanel is created on the EDT, so howcome the redraw-method itself is NOT being run on the EDT (you can see it on the console)? And also, does it matter here? Complicated... (hint: it may have to do with a Thread)

So, in your original code, all that you will see is in the go() method when the frame is being made visible, but that's all. Therefore, you can delete the whole MyDrawPaen class, since it is not being used at all.
I am Arthur, King of the Britons. And this is a tiny ad:
global solutions you can do in your home or backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!