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();
}
}
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 intoinvokeLater(), 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 thatput your
BufferedImageas Icon to the JLabel