• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Animating Swing JComponents

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, title probably isn't helpful. But here's the scoop. I know this is probably a dumb question, but I've never worked with threads in Java, and want to make sure I go about this properly.
I currently have an applet which is a combination of JComponents, which I define paintComponents for, and can add JComponents to. My main drawing canvas supports drawing, adding components with mouseclicks, and most interactivity. My problem is, when I animate something (without threads), it's like doLayout() isn't being called, and all components on this component's layout manager don't appear while animating. Animation is very crude using a loop, Thread.sleep, and paintComponent. Which is why I want to thread it.
The problem gets very bad, when I have a machine that takes in inputs, and provides output. The input happens to be a JTextArea, and while the machine is "churning", it's supposed to look like this JTextArea is getting slowly consumed by the machine. My problem is, my crude animation means none of the JComponent family are displayed during animation, and hence, no machine eating the JTextArea.
So, what I want to know, is how can I Thread this thing and have it animate properly. If you need to know the structure, it's a JApplet, with a JComponent laid on top of it, and the animation occurs within the JComponent. In the JComponent I've defined paintComponent. Nothing else, well nothing else pertaining to drawing to the Graphics context.
I'm sorry if I'm too descriptive. I was having a hard time writing this succinctly. I appreciate any help you can provide!
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You don't need to use sleep in a loop to get animation using Swing. Look at the javax.swing.Timer class. It should be helpful in your case.
Regards,
Manfred.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic