📄 snap.java
字号:
package console;
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.event.MouseEvent;
public class Snap extends JFrame implements Runnable,
java.awt.event.MouseListener, MouseMotionListener {
private DataInputStream in;
private Thread t;
private Socket socket;
private Socket tempSocket;
private ImageIcon icon;
private JLabel lab;
private String ipAddress;
public void init() {
lab = new JLabel();
lab.setBackground(Color.black);
icon = new ImageIcon("25.jpg");
lab.setIcon(icon);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(lab, BorderLayout.CENTER);
this.addMouseMotionListener(this);
this.addMouseListener(this);
this.addKeyListener(new keyAdapet());
setSize(600, 400);
setVisible(true);
}
public Snap(String ipAddress) {
super("远程监控");
init();
this.ipAddress = ipAddress;
t = new Thread(this);
t.start();
}
class keyAdapet extends KeyAdapter
{ //键盘监听适配器
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == 27) { //按ESC键
Object[] options = {
"确定",
"取消"};
int n = JOptionPane.showOptionDialog(null,
"是否退出程序?",
"远程监控系统",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, //don't use a custom Icon
options, //the titles of buttons
options[0]);
if (0 == n) {
System.exit(0);
}
}
}
}
public void run() {
try {
socket = new Socket(ipAddress, 1978);
System.out.println("开始连接");
DispalyImage display = new DispalyImage(socket);
display.start();
tempSocket = socket;
} catch (Exception e) {
System.out.println(e.getMessage());
// System.exit(1);
}
}
public void mouseClicked(MouseEvent e) {
System.out.println("双击了鼠标");
int x = e.getX();
int y = e.getY();
if (tempSocket != null) {
new CommandMsg("2", tempSocket, x, y).start();
}
}
public void mousePressed(MouseEvent e) {
if (e.BUTTON1 == MouseEvent.BUTTON1) {
System.out.println("你按了鼠标左键~~~~~~~~~~~");
int x = e.getX();
int y = e.getY();
if (tempSocket != null) {
new CommandMsg("3", tempSocket, x, y).start();
}
}
}
public void mouseReleased(MouseEvent e) {
System.out.println("你按下了鼠标~~~~~~~~~~~");
int x = e.getX();
int y = e.getY();
if (tempSocket != null) {
new CommandMsg("4", tempSocket, x, y).start();
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {
System.out.println("鼠标移动中~~~~~~~~~");
int x = e.getX();
int y = e.getY();
if (tempSocket != null) {
new CommandMsg("1", tempSocket, x, y).start();
}
}
//发送控制信息
class CommandMsg extends Thread {
private int x = 0;
private int y = 0;
private Socket s;
private String eventType = "1";
private PrintWriter out = null;
private CommandMsg(String type, Socket socket) {
this.eventType = type;
this.s = socket;
try {
out = new PrintWriter(
new BufferedOutputStream(s.getOutputStream()), true);
} catch (Exception ex) {
}
}
private CommandMsg(String type, Socket socket, int x, int y) {
this.eventType = type;
this.x = x;
this.y = y;
s = socket;
s = socket;
try {
out = new PrintWriter(
new BufferedOutputStream(s.getOutputStream()), true);
} catch (Exception ex) {
}
}
public void run() {
out.println(eventType + "," + x + "," + y);
out.flush();
}
}
//显示图片
class DispalyImage extends Thread {
private Socket socket;
private Image image;
private boolean flag;
private BufferedInputStream imgStream = null;
//构造函数
public DispalyImage(Socket ss) {
socket = ss;
flag = true;
}
public void run() {
while (flag) {
byte[] buf = new byte[102400];
try {
imgStream = new BufferedInputStream(
socket.getInputStream());
imgStream.read(buf);
ImageIcon icon = new ImageIcon(Toolkit.
getDefaultToolkit().
createImage(buf));
lab.setIcon(icon);
File file = new File("1.jpg");
FileOutputStream fileOut = new FileOutputStream(file);
fileOut.write(buf);
fileOut.close();
repaint();
setVisible(true);
System.out.println("读取图象成功!");
} catch (Exception ex) {
ex.printStackTrace();
flag = false;
}
}
System.out.println("服务器停止");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -