我实施的移动文本自动收录器没有顺利移动,并且当并行执行大量GUI操作时,问题更严重。
我通过创建自己的绘画图像来使用手动双缓冲
在数据更改时,我使用以下代码更新我的缓冲图像:
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);
}
然后我使用drawText(..)在其上绘制所需的文本,然后调用repaint()。
并行地,每10毫秒,异步线程计算图像的下一个X位置,然后调用repaint()。
我的主要组件扩展了JComponet,并覆盖了paintComponent(),如下所述:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(baseImage, scrollTextX, 0, null);
}
是否有任何技巧可以使运动变得更好? 如何使自动收报机运动不受其他GUI操作的影响?
答案 0 :(得分:0)
我猜这是在Swing应用程序中使用的?然后我认为你应该从AWT事件调度线程进行UI更新。您可以使用invokeLater类中的SwingUtilities方法执行此操作。