0

I am having a bit of problem with changing and Icon in a JLabel dynamically. First of all, what i am trying to do is basically simulating a screen sharing operation. On the client side, I am taking screenshots every second, sending them to the server. On the server side, I am trying to open these pictures in a simple GUI.

I am able to send the pictures without problem and I am also able to get them without problem as well. However, the GUI code I have written cannot open the pictures. More specifically, If there is a picture, it is able to open it, but it does not open another picture that has come.

What I am doing in the server side is, as soon as a picture gets to the server, I am saving it with a predetermined name. And then I am able to open the picture with Windows' own picture photo viewer. In fact, as soon as a new picture comes, photo viewer updates itself and shows the new screenshot.

However, I am having trouble opening the screenshots in a JFrame. I have written a program to take the screenshots in jpg format, send them to the server and then open them in a GUI. however i am having problems with the opening in the GUI part. From what I have understood, it does not open the files that are coming by from the client.

Below are my codes, any help will be much appreciated.

Server side,

 package ScreenCap;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import FileServer.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import test.TsgIcons;


/**
 *
 * @author busra
 */
public class ScreenCapServer extends Thread{
    String filePath;
    int portNumber;
    FileServer screenCapServer;
    ServerSocket getFileServer; 
    Socket getFile; 
    InputStream in;
    FileOutputStream fileOutputStream;
    TsgIcons screenShotIcons;

    public ScreenCapServer(String path, int port) {
        this.filePath = path;
        this.portNumber = port;
        this.screenShotIcons = new TsgIcons();
    }


    public static void waitTime(long millisecond){  
        long max = millisecond;  
        for(long i = 0;  i < max; i++){  
            for(long j = 0;  j < max; j++){  

            }  
        }  
    }      

    public void run() {
        while(true) {
            try {
                for(int i = 0; i < 10; i++) {
                    getFileServer = new ServerSocket(portNumber);
                    getFile = getFileServer.accept();
                    in = getFile.getInputStream();
                    fileOutputStream = new FileOutputStream(filePath + "\\" + i + ".jpg");
                    byte [] buffer = new byte[64*1024]; 
                    int bytesRead = 0;
                    while ( (bytesRead = in.read(buffer)) != -1) {
                        fileOutputStream.write(buffer, 0, bytesRead);
                    }                

                    in.close();
                    fileOutputStream.close();
                    getFileServer.close();
                    screenShotIcons.update();
                }
            } catch (IOException ex) {
                ex.printStackTrace(); 
            }
        }   

    }


}

GUI,

    package test;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

public class TsgIcons extends JFrame implements ActionListener {
    protected Timer timer;
    protected JLabel viewIcon;
    private String[] SMILEY = {"orig_screen"};
    private String BUTTON = "Button";
    private int timeCount;
    private int iconNumber;
    private String image;

    public TsgIcons() {
        this(1, 100);
    }

    public TsgIcons(int initialTime, int delay) {
        super("TSG Smileys");
        this.timeCount = initialTime;
        this.iconNumber = this.timeCount % this.SMILEY.length;
        this.image = "TransferredScreenShots\\" + this.SMILEY[this.iconNumber] + ".jpg";
        this.viewIcon = new JLabel();
        this.viewIcon.setIcon(new javax.swing.ImageIcon(this.image));
        this.timer = new Timer(delay, this);
        this.init();
    }

    protected void init() {
        JButton button = new JButton("Start / Stop");
        button.setActionCommand(BUTTON);
        button.addActionListener(this);
        this.viewIcon.setHorizontalAlignment(JLabel.CENTER);
        this.getContentPane().add(button, BorderLayout.SOUTH);
        this.getContentPane().add(this.viewIcon, BorderLayout.CENTER);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocation(250, 250);
        this.pack();

        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if ( BUTTON.equals(e.getActionCommand()) )  { // test if the button clicked
            if ( this.timer.isRunning() ) {
                this.timer.stop();
            } else {
                this.timer.start();
            }
        } else
        {   this.timeCount++;
                this.iconNumber = this.timeCount % this.SMILEY.length;
                this.image = "TransferredScreenShots\\" + this.SMILEY[this.iconNumber] + ".jpg";
                this.viewIcon.setIcon(new javax.swing.ImageIcon(this.image));
        }
    }

    public void update() {
        this.timeCount++;
        this.iconNumber = this.timeCount % this.SMILEY.length;
        this.image = "TransferredScreenShots\\" + this.SMILEY[this.iconNumber] + ".jpg";
        this.viewIcon.setIcon(new javax.swing.ImageIcon(this.image));

    }

    public static void main(String argv []) {
        new TsgIcons();
    }
}
Orion
  • 19
  • 3
  • 2
    Too much code. Post relevant parts only please. – Maroun May 04 '13 at 13:56
  • actually these are the relevant parts. Code is even longer with other parts. I am not sure what is the problem but when on the fly, I am not able to open pictures one by one. – Orion May 04 '13 at 14:02
  • 1
    *"these are the relevant parts."* The moment you work out what the relevant parts are, you'll be 90% of the way to solving it. For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson May 04 '13 at 14:07
  • maybe the client part is not so important. But I am not sure how you guys can (if you want to) try the code without the client. – Orion May 04 '13 at 14:16
  • @AndrewThompson well it is not a problem about changing the icons dynamically, it is a problem about changing the icon while the icon is being rewritten by another code bunch, in this case the "server" code. But nevermind, I solved the problem. thanks for lecturing me instead of trying to help. – Orion May 06 '13 at 11:24

0 Answers0