我正在开发一个用 Java 编写的并发 Canvas ,这将使用户认为他们是在 Canvas 上并行绘图。
为了实现用户感知的并行性,我让他们创建这些 Runnable 对象,我继续使用 SwingUtilities.invokeLater() 将它们放在 EventQueue 上。
为了测试它,我使用几个线程模拟了用户,并在每次调用 invokeLater() 之间添加了一点延迟(大约 50 毫秒),以查看它是否真的看起来像绘图正在发生平行线。
问题是,虽然它在 invokeLater() 调用之间添加的延迟下工作正常,但消除延迟会导致绘图有时会正确绘制,有时会部分绘制和消失,而在其他时间则不会绘制。
我对可能出现的问题感到很困惑,所以如果有人有任何想法,请告诉我。
下面是注释掉延迟的代码:
public void run(){
//add tasks on to the event queue of the EDT
for(int i = 0; i<numLines; i++){
DrawLineTask task = new DrawLineTask(g, x1, y1+i, x2, y2+i, lineColor);
SwingUtilities.invokeLater(task);
// try {
// Thread.sleep(new Double(Math.random()*50).longValue());//random sleeping times to make it appear more realistic
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
}
干杯
编辑:这是请求的 DrawLineTask 代码。它非常简单,因为它只是 Runnable 类的扩展,使用标准 Java 函数在给定参数下绘制一条线。
public class DrawLineTask implements Runnable {
Graphics g;
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
Color color = Color.BLACK;
public DrawLineTask(Graphics g, int x1, int y1, int x2, int y2){
this.g = g;
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public DrawLineTask(Graphics g, int x1, int y1, int x2, int y2, Color color){
this.g = g;
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
}
@Override
public void run() {
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}
}
最佳答案
据我所知,您不应该保留对 Graphics 对象的引用并在需要时在其上绘制。相反,您应该等待 Swing 调用 paintComponent()
方法,然后在该方法中进行绘图。
因此,您的任务应该只更改组件的状态,并请求异步或同步重绘(使用 repaint()
或 paintImmediately()
)。然后,Swing 将调用带有图形对象的 paintComponent()
方法,您可以根据组件的状态使用该对象绘制适当的线条。
参见 http://java.sun.com/products/jfc/tsc/articles/painting/了解更多详情和解释。
关于java - SwingUtilities.invokeLater 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10863098/