Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6795601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:21:59+00:00 2026-05-26T18:21:59+00:00

I’m getting images through sockets. Now, I’m actually getting it, but only once as

  • 0

I’m getting images through sockets. Now, I’m actually getting it, but only once as I expected.

I have checked out some classes you can add events to, such as the ImageReader but it wont communicate with my JFrame. When I finalize my frame, it doesn’t seem to update at all.

Server:

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();
    }

}

What should I do? What’s the best practice?


EDIT 11-11-11 16:19

I have followed mKorbel’s answer and it seems to work better, but I’m still getting errors.

My client script:

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) {}
}

I’m getting an IndexOutOfBoundsException at this line: ImageIO.write(screenshots[0], "png", os); where I write the output to the outputstream.

It seems that the while loop just run 3 times. Why just 3 times? And why does it gives an IndexOutOfBoundsException?


EDIT 11-11-11 16:38

It seems I’ve sorted it, however, my label won’t update. Any idea’s?

Server:

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();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:22:00+00:00Added an answer on May 26, 2026 at 6:22 pm

    because you have some issues with Concurency in Swing, put that to the Background Task

    • SwingWorker, similair example in the tutorial, in this case is GUI still accesible and doesn’t waiting for end of Task(s)

    • Runnable#Thread, but output to the GUI must be wrapped into invokeLater(), in this case is GUI still accesible and doesn’t waiting for end of Task(s)

    • simple wrap that to the invokeLater(), but in this case is GUI freeze isn’t accesible and waiting for end of Task(s), but this way is wrong, simply don’t do that

    put your BufferedImage as Icon to the JLabel

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.