How do I let multiple Threads paint onto an AWT component?

為{幸葍}努か 提交于 2019-12-06 09:59:48

Only the GUI thread can paint directly on your component.

So you must call the repaint method.

When you have background computation, to force a fast drawing, you should use the version taking a time as parameter.

Some details from here :

NOTE: If multiple calls to repaint() occur on a component before the initial repaint request is processed, the multiple requests may be collapsed into a single call to update(). The algorithm for determining when multiple requests should be collapsed is implementation-dependent. If multiple requests are collapsed, the resulting update rectangle will be equal to the union of the rectangles contained in the collapsed requests.

You have to send requests to the EDT.

        EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            Rectangle r = myCurrentWorkingThread.getFinishedRectangle();
            myPainter.repaint(r);
        }
    });

The idea is that you won't repaint pixel by pixel, rather giving larger chunks to the worker-threads. As soon as they're finished with a unit of work, they notify the main object (myPainter) that would do the actual work. This construct (EventQueue.invokeLater) will guarantee that it will be on the Event Dispatcher Thread.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!