0

JLabel のアイコンを動的に変更する際に少し問題があります。まず、私がやろうとしているのは、基本的に画面共有操作をシミュレートすることです。クライアント側では、毎秒スクリーンショットを撮り、サーバーに送信しています。サーバー側では、単純な GUI でこれらの画像を開こうとしています。

問題なく写真を送ることができ、写真も問題なく受け取ることができます。しかし、私が書いた GUI コードでは画像を開くことができません。より具体的には、画像があれば開くことはできますが、別の画像が表示された場合は開きません。

私がサーバー側で行っているのは、画像がサーバーに届くとすぐに、あらかじめ決められた名前で保存していることです。その後、Windows 独自の画像フォト ビューアーで画像を開くことができます。実際、新しい画像が来るとすぐに、フォト ビューアーが更新され、新しいスクリーンショットが表示されます。

ただし、JFrame でスクリーンショットを開くのに問題があります。スクリーンショットをjpg形式で取得し、サーバーに送信してGUIで開くプログラムを作成しました。しかし、GUI部分のオープニングに問題があります。私が理解したことから、クライアントからのファイルは開かれません。

以下は私のコードです。どんな助けでも大歓迎です。

サーバ側、

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

0 に答える 0