5

我正在尝试用Java创建一个程序,该程序将一个接一个地显示一组图像,并调整每个图像的框架大小。我正在扩展 JPanel 以显示这样的图像:

public class ImagePanel extends JPanel{

String filename;
Image image;
boolean loaded = false;

ImagePanel(){}

ImagePanel(String filename){
    loadImage(filename);
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    if(image != null && loaded){
        g.drawImage(image, 0, 0, this);
    }else{
        g.drawString("Image read error", 10, getHeight() - 10);
    }
}

public void loadImage(String filename){
    loaded = false;         
    ImageIcon icon = new ImageIcon(filename);
    image = icon.getImage();
    int w = image.getWidth(this);
    int h = image.getHeight(this);
    if(w != -1 && w != 0 && h != -1 && h != 0){
        setPreferredSize(new Dimension(w, h));
        loaded = true;
    }else{
        setPreferredSize(new Dimension(300, 300));
    }
}

}

然后在事件线程中我正在做主要工作:

        SwingUtilities.invokeLater(new Runnable(){

        @Override
        public void run(){
            createGUI();
        }
    });

在 createGUI() 中,我正在浏览一组图像:

        ImagePanel imgPan = new ImagePanel();
    add(imgPan);

    for(File file : files){
        if(file.isFile()){
            System.out.println(file.getAbsolutePath());

            imgPan.loadImage(file.getAbsolutePath());
            pack();

            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        }
    }

问题是我的程序正确地调整了大小,所以图像被正确加载但它不显示任何东西。如果我只显示一张图像,它也适用于最后一张图像。我认为问题是在图像绘制完成之前调用了 Thread.sleep() 。

我怎样才能等待我的 ImagePanel 完成绘画并在那之后开始等待?还是有其他方法可以解决问题?

谢谢!莱昂蒂

4

3 回答 3

6

您的所有代码都在事件调度线程上执行。这有效地导致所有用户交互进入休眠状态,因为事件调度线程负责所有用户交互——输入(事件)和输出(绘画)。

您需要在 EDT 之外等待。您需要知道如何在 EDT 内外执行事件。为此,您可以创建一个新的Runnable,然后调用new Thread(runnable)以在 EDT 之外执行它或SwingUtilities.invokeLater(runnable)使其在 EDT 上执行。与 Swing 组件的所有交互都需要在 EDT 上进行,因为 Swing 对象不是线程安全的。所有睡眠、等待、文件访问、网络访问、数据库访问或任何其他可能在不确定的时间段内阻塞的事情都必须在 EDT 之外进行。

Stack Overflow 上存在许多与 Event Dispatch Thread 相关的问题。我建议您查看这些以查找更多信息和代码示例,以便以不同的方式执行您希望执行的操作。

于 2010-12-09T19:34:06.410 回答
1

不要让线程休眠,而是使用计时器并将下一个图像作为事件启动。

于 2010-12-09T19:31:04.043 回答
1

听起来您希望图像在加载时一张一张地出现(即在网页中),对吗?如果是这样,您将不会使用当前代码获得这种效果,因为在加载所有图像之前您的 UI 不会更新,这是因为您正在 EDT(事件调度线程)上加载图像,该线程与“会画的。绘画发生在“有时间”的情况下,在这种情况下,这意味着在加载所有图像之后。

为了解决您的问题,我建议您创建一个SwingWorker的子类,例如:

public class MyImageLoader extends SwingWorker<Void, String>
{
    // Is executed in background thread, EDT can meanwhile load and paint images
    @Override
    protected Void doInBackground() throws Exception
    {
        for(File file : files)
        {
             if(file.isFile()) // Maybe some more check to see if it's an image
             {
                 publish(file.getAbsolutePath());
                 Thread.sleep(500);
             }
        }
    }

    // Executed on EDT
    @Override
    protected void process(List<String> filePaths)
    {
        for(String path : filePaths)
        {
            // Load ImageIcon to UI
        }
    }
}
于 2010-12-09T19:50:45.373 回答