File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Animating Swing JComponents Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Animating Swing JComponents" Watch "Animating Swing JComponents" New topic

Animating Swing JComponents

Tom West

Joined: Dec 10, 2001
Posts: 2
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!
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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.
I agree. Here's the link:
subject: Animating Swing JComponents
It's not a secret anymore!