首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >每次收到图像时更新窗口

每次收到图像时更新窗口
EN

Stack Overflow用户
提问于 2011-11-11 11:14:59
回答 1查看 160关注 0票数 1

我正在通过套接字获取图像。现在,我实际上得到了它,但只有一次,如我所料。

我已经检查了一些可以添加事件的类,例如ImageReader,但是它不会与我的JFrame通信。当我最后确定我的帧时,它似乎一点也不更新。

服务器:

代码语言:javascript
复制
public static void main(String[] args) {
    try {
        DefaultWindow window = new DefaultWindow();
        window.frame.setVisible(true);

        ServerSocket ss = new ServerSocket(4305);
        Socket cs = ss.accept(); // only 1 client is assumed

        // this bit should be in some sort of complete event
        BufferedImage bi = ImageIO.read(cs.getInputStream());

        window.image.setIcon(new ImageIcon(bi));
        window.frame.validate();
        window.frame.repaint();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

我该怎么办?最好的做法是什么?

编辑11-11-11 16:19

我遵循了mKorbel的回答,似乎效果更好,但我仍然会犯错误。

我的客户脚本:

代码语言:javascript
复制
public static void main(String[] args) {
    try {
        Socket cs = new Socket(InetAddress.getByName("localhost"), 4305);
        OutputStream os = cs.getOutputStream();

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gs = ge.getScreenDevices();
        BufferedImage[] screenshots = new BufferedImage[gs.length];

        DisplayMode mode;
        Rectangle bounds;

        while(true) {
            try {
                for(int i = 0; i < gs.length; i++)
                {
                    mode = gs[i].getDisplayMode();
                    bounds = new Rectangle(0, 0, mode.getWidth(), mode.getHeight());
                    screenshots[i] = new Robot(gs[i]).createScreenCapture(bounds);
                }

                if(screenshots[0] != null) {
                    ImageIO.write(screenshots[0], "png", os);
                }
                os.flush();

                Thread.sleep(500);
            } catch (AWTException e) {
            } catch (InterruptedException e) {}
        }
    } catch(IOException e) {}
}

我在这一行得到一个IndexOutOfBoundsException:ImageIO.write(screenshots[0], "png", os);,其中我将输出写入输出流。

看起来while循环只运行了3次。为什么只有三次?为什么它会给出一个IndexOutOfBoundsException?

编辑11-11-11 16:38

似乎我已经整理好了,但是,我的标签不会更新。有什么想法吗?

服务器:

代码语言:javascript
复制
public static void main(String[] args) {
    try {
        final DefaultWindow window = new DefaultWindow();
        window.frame.setVisible(true);

        ServerSocket ss = new ServerSocket(4305);
        final Socket cs = ss.accept();

        SwingWorker<ImageIcon, Void> sw = new SwingWorker<ImageIcon, Void>() {
            BufferedImage bi = ImageIO.read(cs.getInputStream());

            BufferedImage bis;
            Graphics2D graphics2D;
            ImageIcon icon = null;

            @Override
            protected ImageIcon doInBackground() throws Exception {
                bis = new BufferedImage(window.image.getWidth(), window.image.getHeight(), BufferedImage.TYPE_INT_ARGB);

                graphics2D = bis.createGraphics();
                graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                graphics2D.drawImage(bi, 0, 0, window.image.getWidth(), window.image.getHeight(), null);
                graphics2D.dispose();

                if(icon != null) {
                    icon.getImage().flush();
                }

                icon = new ImageIcon(bis);
                return icon;
            }

            protected void done() {
                window.image.setIcon(icon);
                window.frame.validate();
                window.frame.repaint();
            }
        };

        sw.execute();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-11 11:33:02

因为您对摆动中的一致性有一些问题,所以把它放到后台任务中

  • SwingWorker,本教程中类似的例子,在本例中是GUI仍然是可访问的,并且不等待任务的结束
  • Runnable#Thread,但是对GUI的输出必须包装成invokeLater(),在这种情况下,GUI仍然是可访问的,并且不等待任务的结束。
  • 简单地将其包装到invokeLater(),但在本例中,GUI冻结是不可访问的,等待任务结束,但这种方式是错误的,只是不要这样做

将您的BufferedImage作为图标放到JLabel

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8093173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档