远程鼠标移动不稳定

时间:2015-05-19 23:39:20

标签: java sockets mouseevent awtrobot

我使用mouselistener和机器人类在一台计算机上移动鼠标并将光标移动到另一台计算机上它是我正在处理的远程桌面项目的前身。我已经偶然发现的一个问题是鼠标运动不稳定。我已经超过了我的代码一百次,我似乎无法弄清楚出了什么问题。

以下是将鼠标移动发送到客户端的3个服务器文件。

TC_ServerListener.java

import java.awt.BorderLayout;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;


public class TC_ServerListener {
    private JFrame mainTCframe = new JFrame();
    private JDesktopPane mainDesktopPane = new JDesktopPane();

    public static void main(String args[]){
        new TC_ServerListener().startServer();
    }

    public void startServer() {

        try {
            setupGUI();

            ServerSocket serverSocket = new ServerSocket(1111);
            System.out.println("Waiting for clients.");
            while (true) {
                Socket clientSocket = serverSocket.accept();
                new TC_ServerStarter(clientSocket, mainDesktopPane);
            }
        } catch (IOException ex) {
            Logger.getLogger(TC_ServerListener.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void setupGUI(){
            mainTCframe.add(mainDesktopPane,BorderLayout.CENTER);
            mainTCframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainTCframe.setExtendedState(mainTCframe.getExtendedState()|JFrame.MAXIMIZED_BOTH);
            mainTCframe.setVisible(true);
    }
}

TC_ServerStarter.java

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.beans.PropertyVetoException;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;


public class TC_ServerStarter extends Thread {

    int counter = 0;

    JPanel listenerPanel = new JPanel();
    JInternalFrame listenerFrame = new JInternalFrame("TestClient", true, true, true);
    Rectangle screenRect = new Rectangle();

    GraphicsEnvironment graphicsEnv;
    GraphicsDevice graphicsDev;
    Dimension dim;
    Dimension clientDim;
    Dimension serverDim;

    static DataOutputStream kbOut;
    static DataOutputStream mouseMoveOut;
    static DataOutputStream mouseClickOut;
    static DataOutputStream mouseDragOut;

    Socket clientSocket;
    JDesktopPane mainDesktopPane;

    PrintWriter writer;
    Scanner dataScanner;

    public TC_ServerStarter(Socket cs, JDesktopPane mdp) {
        clientSocket = cs;
        mainDesktopPane = mdp;

        start();
    }

    public void setupGUI() {
        listenerFrame.setLayout(new BorderLayout());
        listenerFrame.getContentPane().add(listenerPanel,BorderLayout.CENTER);
        listenerFrame.setSize(100,100);
        mainDesktopPane.add(listenerFrame);

        try {
            listenerFrame.setMaximum(true);
            } catch (PropertyVetoException ex) {
            Logger.getLogger(TC_ServerStarter.class.getName()).log(Level.SEVERE, null, ex);
        }
        listenerPanel.setFocusable(true);
        listenerFrame.setVisible(true);
    }



        @Override
        public void run() {

            try {
                setupGUI();

                String clientIP = clientSocket.getInetAddress().getHostAddress();
                System.out.println("Client connected from " + clientIP);

                writer = new PrintWriter(clientSocket.getOutputStream());
                dataScanner = new Scanner(clientSocket.getInputStream());

                int screenHeight = dataScanner.nextInt();
                int screenWidth = dataScanner.nextInt();
                System.out.println("Setting screen size: " + screenHeight + "x" + screenWidth);
                clientDim = new Dimension(screenHeight, screenWidth);

                new KvmSender(clientSocket, listenerPanel, clientDim);


            } catch (IOException ex) {
                Logger.getLogger(TC_ServerStarter.class.getName()).log(Level.SEVERE, null, ex);
              }
            }
        }

KvmSender.java

import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JPanel;

class KvmSender implements KeyListener, MouseMotionListener, MouseListener {

    private Socket clientSocket;
    private JPanel listenerPanel;
    private Dimension clientDim;
    private PrintWriter clientWriter;

    public KvmSender(Socket cs, JPanel lp, Dimension cd) {
        clientSocket = cs;
        listenerPanel = lp;
        clientDim = cd;

        listenerPanel.addKeyListener(this);
        listenerPanel.addMouseListener(this);
        listenerPanel.addMouseMotionListener(this);
        try {
            clientWriter = new PrintWriter(clientSocket.getOutputStream());
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        System.out.println("Ready to send commands.");
    }

    @Override
    public void keyTyped(KeyEvent ke) {
    }

    @Override
    public void keyPressed(KeyEvent ke) {
    }

    @Override
    public void keyReleased(KeyEvent ke) {
    }

    @Override
    public void mouseDragged(MouseEvent me) {
    }

    @Override
    public void mouseMoved(MouseEvent me) {
        try {
            double scaleX = clientDim.getWidth() / listenerPanel.getWidth();
            double scaleY = clientDim.getHeight() / listenerPanel.getHeight();
            int mouseX = (int)(me.getX() * scaleX);
            int mouseY = (int)(me.getY() * scaleY);

            clientWriter.println(1);
            clientWriter.println(mouseX);
            clientWriter.println(mouseY);
            System.out.println("Sending mouse: " + mouseX + "," + mouseY);
            clientWriter.flush();

        } catch (NullPointerException ex) {
            System.out.println("Error writing mouse data to server");
        }
    }

    @Override
    public void mouseClicked(MouseEvent me) {
    }

    @Override
    public void mousePressed(MouseEvent me) {
    }

    @Override
    public void mouseReleased(MouseEvent me) {
    }

    @Override
    public void mouseEntered(MouseEvent me) {
    }

    @Override
    public void mouseExited(MouseEvent me) {
    }

}

然后是2个客户端文件。

TC_ClientStarter.java

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class TC_ClientStarter extends Thread {

    private final static String SERVER_IP = "192.168.0.50";

    JPanel clientPanel = new JPanel();
    JFrame clientFrame = new JFrame();

    GraphicsEnvironment graphicsEnv;
    Rectangle screenRect;
    Dimension screenSize;

    static PrintWriter dataWriter;

    Robot tcRobot;

    Socket clientSocket;

    public void setupGUI() {

        clientFrame.add(clientPanel);
        clientFrame.setSize(200,200);
        clientFrame.setVisible(true);
        clientFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        clientPanel.requestFocusInWindow();
    }

    public static void main(String[] args) {
        new TC_ClientStarter().setupClient();
    }

    public void setupClient() {

        try {
            screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            screenRect = new Rectangle(screenSize.height, screenSize.width);

            clientSocket = new Socket(SERVER_IP, 1111);
            System.out.println("Connected to Commander.");

            System.out.println("Sending screen size: " + screenRect.height + "x" + screenRect.width);
            dataWriter = new PrintWriter(clientSocket.getOutputStream());
            dataWriter.println(screenRect.height);
            dataWriter.println(screenRect.width);
            dataWriter.flush();

            tcRobot = new Robot();

            setupGUI();

            new TC_ClientReceiver(clientSocket, tcRobot);


            } catch (ConnectException ece) {
            System.out.println("Failed to connect to server: " + SERVER_IP);
        } catch (IOException | AWTException eio) {
            Logger.getLogger(TC_ClientStarter.class.getName()).log(Level.SEVERE, null, eio);
        }
    }
}

TC_ClientReceiver.java

import java.awt.Robot;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

class TC_ClientReceiver extends Thread {

    private Socket clientSocket;
    private Robot tcRobot;

    private Scanner clientScanner;

    private int mouseX, mouseY;

    public TC_ClientReceiver(Socket cs, Robot tcr) {
        clientSocket = cs;
        tcRobot = tcr;
        start();
    }

    @Override
    public void run(){

        try {
            clientScanner = new Scanner(clientSocket.getInputStream());
            System.out.println("Waiting for commands.");
            while (true) {

                int commandID = clientScanner.nextInt();
                //System.out.println("CommandID: " + commandID);
                switch (commandID) {

                    case 1:
                        System.out.println("Mouse move");
                        mouseX = clientScanner.nextInt();
                        mouseY = clientScanner.nextInt();
                        tcRobot.mouseMove(mouseX, mouseY);
                        System.out.println(mouseX + "," + mouseY);
                        break;
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(TC_ClientReceiver.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}

0 个答案:

没有答案