📄 remotecontrolclient.java
字号:
package com;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.net.Socket;
import java.util.zip.ZipInputStream;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
public class RemoteControlClient extends JLabel implements Runnable {
BufferedInputStream input;
PrintWriter output;
static JFrame frame;
BufferedImage image;
ImageIO cimage;
Socket client;
String MousePressed="1";
String MouseReleased="2";
String MouseMoved="3";
String KeyPressed="4";
String KeyReleased="5";
String friendsIp;
int port;
boolean flg=true;
public void client(String ip,int friendsPort) {
try {
friendsIp=ip;
port=friendsPort;
frame = new JFrame("Remote Control Client");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
try {
client.close();
flg=false;
} catch (Exception ex) {
ex.getMessage();
}
}
});
frame.getContentPane().setLayout(new BorderLayout());
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(640, 480);
Dimension thesize = frame.getSize();
frame.setLocation((screen.width - thesize.width) / 2,
(screen.height - thesize.height) / 2);
new Thread(this).start();
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
sendMouse(MouseEvent.MOUSE_PRESSED,e);
}
public void mouseReleased(MouseEvent e) {
sendMouse(MouseEvent.MOUSE_RELEASED,e);
}
});
this.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e) {
sendMouse(MouseEvent.MOUSE_DRAGGED,e);
}
public void mouseMoved(MouseEvent e) {
sendMouse(MouseEvent.MOUSE_MOVED,e);
}
});
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
sendKey(KeyEvent.KEY_PRESSED,e);
}
public void keyReleased(KeyEvent e) {
sendKey(KeyEvent.KEY_RELEASED,e);
}
});
JScrollPane scroll = new JScrollPane(this);
frame.getContentPane().add(scroll, BorderLayout.CENTER);
frame.setVisible(true);
} catch (Exception e) {
System.out.println(e.toString());
System.exit(0);
}
}
public void run() {
try {
client = new Socket(friendsIp, port);
input = new BufferedInputStream(client.getInputStream());
output = new PrintWriter(client.getOutputStream());
/*
* String the = JOptionPane.showInputDialog(this, "Please input
* target server:", "Targe Server", JOptionPane.QUESTION_MESSAGE);
*/
while (flg) {
// 栤戣偑偁傝傑偡丅偙偺儊僢僜僪client.isClosed()側偄
if (client == null) {
} else {
getScreen(input);
// Thread.sleep(2);
}
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
public void paint(Graphics g) {
super.paint(g);
if (image != null) {
g.drawImage(image, 0, 0, this);
}
}
public void sendMouse(int type,MouseEvent e) {
String command;
if(type==MouseEvent.MOUSE_PRESSED){
command=MousePressed+e.getModifiers()
+System.getProperty("line.separator");
sendCommand(command);
}else if(type==MouseEvent.MOUSE_RELEASED){
command=MouseReleased+e.getModifiers()
+System.getProperty("line.separator");
sendCommand(command);
}else if(type==MouseEvent.MOUSE_DRAGGED){
command=MouseMoved+"|"+e.getX()+"|"+e.getY()
+System.getProperty("line.separator");
sendCommand(command);
}else if(type==MouseEvent.MOUSE_MOVED){
command=MouseMoved+"|"+e.getX()+"|"+e.getY()
+System.getProperty("line.separator");
sendCommand(command);
}
}
public void sendKey(int type,KeyEvent e) {
String command;
if(type==KeyEvent.KEY_PRESSED){
command=KeyPressed+e.getKeyChar()
+System.getProperty("line.separator");
sendCommand(command);
}else if(type==KeyEvent.KEY_RELEASED){
command=KeyReleased+e.getKeyChar()
+System.getProperty("line.separator");
sendCommand(command);
}
}
public synchronized void sendCommand(String strCommand) {
output.write(strCommand);
}
private void getScreen(BufferedInputStream input) {
try {
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(input);
image = decoder.decodeAsBufferedImage();
this.updateUI();
} catch (Exception e) {
try {
client.close();
client = null;
} catch (Exception ex) {
System.out.println(ex.toString());
}
System.out.println(e.toString());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -