Reputation: 567
The moving text ticker I implemented doesn't move in smooth way, and when heavy GUI operations executed in parallel, the problem is worse.
I'm using manual double buffering by creating my own image that will painted.
On data change, I update my Buffered Image using the following code:
public static BufferedImage createBufferedImage(int w, int h) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
return gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
}
Then I draw the required text on it using drawText(..), and calling repaint() after that.
In parallel, every 10 milliseconds, an asynchronous thread calculates the next X position for the image, and then calls repaint().
My main components extends JComponet, and overrides paintComponent(), as described below:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(baseImage, scrollTextX, 0, null);
}
Are there any tricks to make the movement better?
How can I make the ticker movement not influenced by other GUI operations?
Upvotes: 2
Views: 305
Reputation: 144
I'm guessing that this is used in a Swing application? Then I think you should do the UI updating stuff from the AWT event dispatching thread. You can do this by using the invokeLater method from the SwingUtilities class.
Upvotes: 0