我实现的移动文本行情自动收录器无法流畅运行,并且当并行执行繁重的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操作的影响?

最佳答案

我猜这是在Swing应用程序中使用的吗?然后,我认为您应该从AWT事件分配线程中进行UI更新。您可以使用invokeLater类中的SwingUtilities方法来执行此操作。

10-07 13:23