在Java中异步加载基于网络的图像

发布于 2024-11-01 03:33:46 字数 326 浏览 3 评论 0原文

我必须能够加载和绘制位于基于网络的驱动器上的 X 数量的图像。 我需要帮助找到异步加载图像的方法。

 java.net.URL Loc = new URL("http://auroragm.sourceforge.net/GameCover/GameCases/Mass-Effect.png");
    JLabel lbl = new JLabel();
    lbl.setIcon((anotherIcon = new ImageIcon(Loc)));

上面是在 GUI 线程上加载的一张图像,因此如果要加载另外 20 个图像,就会冻结。任何帮助将不胜感激

I have to be able to load and draw X amount of images located on a network based drive.
I need help finding a way to load the images asynchronously.

 java.net.URL Loc = new URL("http://auroragm.sourceforge.net/GameCover/GameCases/Mass-Effect.png");
    JLabel lbl = new JLabel();
    lbl.setIcon((anotherIcon = new ImageIcon(Loc)));

The above is one image which loads on the GUI thread and thus would freeze if 20 more were to be loaded. Any help would be appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眸中客 2024-11-08 03:33:46

在单独的线程中加载图像。请将以下代码视为伪代码:

final java.net.URL Loc = new URL("http://.../Mass-Effect.png");
Thread t = new Thread(new Runnable() {
    public void run() {
        Object content = Loc.getContent();
        // content would be probably some Image class or byte[]

        // or:
        // InputStream in = Loc.openStream();
        // read image from in
    }
);

Load the images in separate thread. Please treat below code as pseudo-code:

final java.net.URL Loc = new URL("http://.../Mass-Effect.png");
Thread t = new Thread(new Runnable() {
    public void run() {
        Object content = Loc.getContent();
        // content would be probably some Image class or byte[]

        // or:
        // InputStream in = Loc.openStream();
        // read image from in
    }
);
彩扇题诗 2024-11-08 03:33:46

简短的回答:您应该在另一个线程上加载图像。

Swing 确实提供了一组很好的类和类。模式:

http://download.oracle.com /javase/6/docs/api/javax/swing/SwingWorker.html

Short answer: you should load the images on another thread.

Swing does provide a nice set of classes & patterns for this:

http://download.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文