This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
First of all, read this. Your repaint() should be done on the EDT. Switch to a javax.swing.Timer instead. You won't need to call Thread.sleep yourself; just schedule the Timer to run at the desired interval.
Secondly, Thread.sleep(10) sleeps 10 milliseconds. In other words, hardly at all. You could just as well remove the call. If you meant to sleep 10 seconds you must use 10000.
Thanks. But the reason i use 10 milliseconds sleep is that I need to move a label from right to left on the screen. so after each sleep, i change the x-coordinate of the label by 5. so it looks as if it is moving.
is there any other way to do it i.e. to make a label move across the screen?
Doesn't even show up with any CPU usage on my system.
Joined: Jan 06, 2011
Thanks Rob for such efforts.
Your code run fine on an independent application.
But since I have to integrate this scroll into an existing application, I need to call repaint() method everytime to update the position. It is not doing it on its own. That is why CPU usage goes up I guess.
I tried using the javax.Swing.Timer class and it reduced the CPU Usage in the normal mode to a great extent. But when the application runs in High Resolution mode (My application has these 2 modes), it is still crashing the application. Is there a way to get past that?
Why do you need to repaint most of your user interface if only one label is moving? How did you implement that label, as a JLabel or as a custom-drawn String? In the first case, calling setLocation should be enough. In the second case you don't need to repaint the entire screen - just the parts that are changed. Instead of repaint(), call repaint(x, y, w, h) where the rectangle of x, y, w and h contains both the old and new location of the label. Or call repaint(x, y, w, h) twice, once for the old location and once for the new one.
Joined: Jan 06, 2011
Rob's a genius!!!
Thanks a ton!!!
Please close the thread.