0

我正在尝试使用玻璃窗格使框架变灰,而单独的线程执行一些图像处理(img proc)。img proc 线程完成后,玻璃窗格应该再次不可见。我已经确认玻璃窗格运行正常,但在 img proc 线程(它确实按预期执行等待和通知工作)开始之前不会发生重新绘制。这是我所拥有的:

GlassPane班级:

class GlassPane extends JComponent implements MouseListener
{
    GlassPane()
    {
        super();
        setLayout(new BorderLayout());
        setOpaque(false); // So that Color's alpha channel is used
        addMouseListener(this);
    }
    @Override
    protected void paintComponent(Graphics g)
    {
        Rectangle bounds = g.getClipBounds();

        g.setColor(new Color(255,255,255,160));

        g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
    }
...
}

在构建框架的组件时:

gPane = new GlassPane();
rootFrame.setGlassPane(gPane);
setGlassPane( false );
...
public void setGlassPane( boolean gray )
{
    if( gray )
        keyCatcher.disable();
    else
        keyCatcher.enable();

    gPane.setVisible(gray);
    gPane.repaint(); // Also tried gPane.repaint(0);
    gPane.invalidate();
}

在按钮的动作监听器中:

...
System.out.println( "Answer button pressed..." );
    ctrlr.grayOut();

    new Thread( new Runnable()
    { 
        public void run(){
            int responses[];
            ImgProc ip = new ImgProc(ctrlr);
            ArrayList<ColorModel> model = ctrlr.getColorModel();
            responses = ip.findResponses( camContent.getCrntImage(), model, (source == ansAndDisplayBtn) );
            ctrlr.setResponses(responses);
            synchronized(lock)
            {
                lock.notifyAll();
                System.out.println( "Notified..." );
            }
        }
    }).start();

    synchronized(lock)
    {
        try {
            System.out.println( "Waiting..." );
            lock.wait();
            System.out.println( "Responses retreived..." );
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        qContent.answer();
        qContent.updateResponses();

        if( scrnMode )
        {
            proj_qContent.answer();
            proj_qContent.updateResponses();
        }
    }
    ctrlr.showFull();
    ...

地点ctrlr.grayOut()和地点ctrlr.showFull()

public void grayOut()
{
    ((MainUI) mainUI).setGlassPane(true);
}

public void showFull()
{
    ((MainUI) mainUI).setGlassPane(false);
}

我已经在 AWT 和 Swing以及执行此类操作的其他线程中阅读了很多此绘画。在我看来,我正在做与那些成功的人一样的事情......我是否缺少一些微妙的东西?

4

1 回答 1

1

This:lock.wait();阻塞事件调度线程,这样就不会发生绘图。我会使用SwingWorker来完成繁重的任务。也就是说,把图像处理doInBackground()和你拥有的东西wait放在done().

// Inform the user the task is running
ctrlr.grayOut();

new SwingWorker<Void, Void>() {
   @Override
   public Void doInBackground() {
       // process the image
       ...
       return null;
   }

   @Override
   protected void done() {
        // Everything done, inform the user
        ...
        ctrlr.showFull();
    }
}.execute();
于 2013-08-07T15:02:14.073 回答