• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Animating Swing JComponents

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic