我正在编写一个用Java编写的并发画布,这将使用户认为他们在画布上并行绘制。
为了实现用户感知的并行性,我让他们创建这些可运行的对象,然后使用SwingUtilities.invokeLater()将这些对象放在SwingUtilities.invokeLater上。
为了测试它,我用几个线程模拟了用户,并在每次调用invokeLater()之间添加了一些延迟(大约50 is ),以查看绘图是否实际上是并行进行的。
问题是,虽然在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);
}
}发布于 2012-06-02 22:13:41
AFAIK,您不应该保留对Graphics对象的引用,并在需要时使用它。相反,您应该等待Swing调用paintComponent()方法,并在此方法中执行绘图。
因此,您的任务应该更改组件的状态,并请求异步或同步重新绘制(使用repaint()或paintImmediately())。然后,Swing将使用Graphics对象调用paintComponent()方法,您可以使用该对象根据组件的状态绘制适当的行。
有关更多细节和解释,请参见http://java.sun.com/products/jfc/tsc/articles/painting/。
https://stackoverflow.com/questions/10863098
复制相似问题