java通过控制鼠标实现屏幕广播 java通过控制鼠标实现屏幕广播的方法
人气:0想了解java通过控制鼠标实现屏幕广播的方法的相关内容吗,在本文为您仔细讲解java通过控制鼠标实现屏幕广播的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,控制,鼠标,屏幕,广播,下面大家一起来学习吧。
本文实例讲述了java通过控制鼠标实现屏幕广播的方法。分享给大家供大家参考。具体分析如下:
在前面一篇《java实现屏幕共享功能实例分析》中提到截取屏幕时是没鼠标,为了看到教师端界面上的鼠标,可以在截取屏幕的时候,把鼠标绘制到每一张截图上去,但是由于截图的时候是一张张截取的,所以看到的鼠标难免会有点卡,之前写了java鼠标操控小程序,可以通过这种方式来看到鼠标的演示。
实现的方式也挺简单的,前面两篇文章分别实现了鼠标控制和不带鼠标的屏幕分享功能,把这两个结合一下就ok了,下面简单分析下。
服务端,将SendScreenImg和SendMouseMessage看作两个工具类,分别监听不同的端口,他们两个都实现了Thread类,我们用线程池ExecutorService类控制他们。
使用了两个端口,因为暂时还不知道该如何吧鼠标信息和图片的信息一起发送,或许可以把图片转换成字节数组的形式,把鼠标的坐标放在数组前面,不过这样的话鼠标可能也会不连贯,因为传送鼠标坐标的速度会比传图片的要快一些,嗯,有空再试试。
客户端类比上面就是了。
下面是代码:
服务端:
主程序:
复制代码 代码如下:
/*
* 屏幕广播类,调用了两个工具类:发送截屏信息的类和发送鼠标的信息类,利用了线程池。
*/
public class BroderCast {
public static void main(String[] args)
{
new BroderCast();
System.out.println("开始");
}
public BroderCast()
{
ExecutorService exector = Executors.newFixedThreadPool(2);
exector.execute(new SendScreenImg());
exector.execute(new SendMouseMessage());
}
}
* 屏幕广播类,调用了两个工具类:发送截屏信息的类和发送鼠标的信息类,利用了线程池。
*/
public class BroderCast {
public static void main(String[] args)
{
new BroderCast();
System.out.println("开始");
}
public BroderCast()
{
ExecutorService exector = Executors.newFixedThreadPool(2);
exector.execute(new SendScreenImg());
exector.execute(new SendMouseMessage());
}
}
发送截图代码:
复制代码 代码如下:
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.imageio.ImageIO;
/*
* 工具类:发送教师端截屏信息给学生端,没有鼠标信息,使用了8002号端口
* 可以在发送的图片上面组件绘制鼠标的信息,从而实现在学生端界面上见到鼠标信息,暂未实现该功能
*
*/
public class SendScreenImg extends Thread
{
public int serverPort=8002;
private ServerSocket serverSocket;
private Robot robot;
public Dimension screen;
public Rectangle rect ;
private Socket socket;
public static void main(String args[])
{
new SendScreenImg().start();
}
public void changeServerPort(int serverPort)
{
if(this.serverPort == serverPort) return ;
try{
this.serverSocket.close(); //有必要先关闭当前端口
this.serverPort = serverPort;
serverSocket = new ServerSocket(this.serverPort);
serverSocket.setSoTimeout(8000000);
}catch(Exception e){}
}
//构造方法 开启套接字连接 机器人robot 获取屏幕大小
public SendScreenImg()
{
try {
serverSocket = new ServerSocket(getServerPort());
serverSocket.setSoTimeout(864000000);
robot = new Robot();
} catch (Exception e) {
e.printStackTrace();
}
screen = Toolkit.getDefaultToolkit().getScreenSize(); //获取主屏幕的大小
rect = new Rectangle(screen); //构造相应大小的矩形
}
@Override
public void run()
{
//实时等待接收截屏消息
while(true){
try {
socket = serverSocket.accept();
ZipOutputStream zip = new ZipOutputStream(new DataOutputStream(socket.getOutputStream()));
zip.setLevel(9); //设置压缩级别
try{
BufferedImage img = robot.createScreenCapture(rect);
zip.putNextEntry(new ZipEntry("test.jpg"));
ImageIO.write(img, "jpg", zip);
if(zip!=null)zip.close();
System.out.println("学生端口已经连接");
} catch (IOException ioe) {
System.out.println("被控端:disconnect");
}
} catch (IOException ioe) {
System.out.println("连接出错");
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
}
}
}
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.imageio.ImageIO;
/*
* 工具类:发送教师端截屏信息给学生端,没有鼠标信息,使用了8002号端口
* 可以在发送的图片上面组件绘制鼠标的信息,从而实现在学生端界面上见到鼠标信息,暂未实现该功能
*
*/
public class SendScreenImg extends Thread
{
public int serverPort=8002;
private ServerSocket serverSocket;
private Robot robot;
public Dimension screen;
public Rectangle rect ;
private Socket socket;
public static void main(String args[])
{
new SendScreenImg().start();
}
public void changeServerPort(int serverPort)
{
if(this.serverPort == serverPort) return ;
try{
this.serverSocket.close(); //有必要先关闭当前端口
this.serverPort = serverPort;
serverSocket = new ServerSocket(this.serverPort);
serverSocket.setSoTimeout(8000000);
}catch(Exception e){}
}
//构造方法 开启套接字连接 机器人robot 获取屏幕大小
public SendScreenImg()
{
try {
serverSocket = new ServerSocket(getServerPort());
serverSocket.setSoTimeout(864000000);
robot = new Robot();
} catch (Exception e) {
e.printStackTrace();
}
screen = Toolkit.getDefaultToolkit().getScreenSize(); //获取主屏幕的大小
rect = new Rectangle(screen); //构造相应大小的矩形
}
@Override
public void run()
{
//实时等待接收截屏消息
while(true){
try {
socket = serverSocket.accept();
ZipOutputStream zip = new ZipOutputStream(new DataOutputStream(socket.getOutputStream()));
zip.setLevel(9); //设置压缩级别
try{
BufferedImage img = robot.createScreenCapture(rect);
zip.putNextEntry(new ZipEntry("test.jpg"));
ImageIO.write(img, "jpg", zip);
if(zip!=null)zip.close();
System.out.println("学生端口已经连接");
} catch (IOException ioe) {
System.out.println("被控端:disconnect");
}
} catch (IOException ioe) {
System.out.println("连接出错");
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
}
}
}
发送鼠标信息:
复制代码 代码如下:
/*
* 工具类:获取鼠标的信息并且发送给学生端
*/
public class SendMouseMessage extends Thread{
private int OPERATE_PORT = 8001;
private ServerSocket server;
private Socket socket;
private String operateStr;
public static void main(String[] args)
{
new SendMouseMessage().start();
}
public SendMouseMessage(){
try {
server = new ServerSocket(OPERATE_PORT);
//JOptionPane.showMessageDialog(null, "已经开始监听");
} catch (IOException e1) {
e1.printStackTrace();
}
}
//多线程 在无线的循环中监听客户端的
public void run()
{
while(true){
Point point = MouseInfo.getPointerInfo().getLocation(); //
operateStr ="Movemouse,"+point.x+","+point.y;
try {
socket = server.accept();
socket.setSoTimeout(1000000);
DataOutputStream output =new DataOutputStream(socket.getOutputStream());
output.write(operateStr.getBytes());
output.flush(); //刷行输出流,并且使所有缓冲的输出字节写出
output.close(); //关闭输出流且释放资源
System.out.println("INFO: "+operateStr);
} catch (IOException e) {
System.out.println("已经停止连接");
break; //断开连接的时候就停止无线循环
}
}
}
}
* 工具类:获取鼠标的信息并且发送给学生端
*/
public class SendMouseMessage extends Thread{
private int OPERATE_PORT = 8001;
private ServerSocket server;
private Socket socket;
private String operateStr;
public static void main(String[] args)
{
new SendMouseMessage().start();
}
public SendMouseMessage(){
try {
server = new ServerSocket(OPERATE_PORT);
//JOptionPane.showMessageDialog(null, "已经开始监听");
} catch (IOException e1) {
e1.printStackTrace();
}
}
//多线程 在无线的循环中监听客户端的
public void run()
{
while(true){
Point point = MouseInfo.getPointerInfo().getLocation(); //
operateStr ="Movemouse,"+point.x+","+point.y;
try {
socket = server.accept();
socket.setSoTimeout(1000000);
DataOutputStream output =new DataOutputStream(socket.getOutputStream());
output.write(operateStr.getBytes());
output.flush(); //刷行输出流,并且使所有缓冲的输出字节写出
output.close(); //关闭输出流且释放资源
System.out.println("INFO: "+operateStr);
} catch (IOException e) {
System.out.println("已经停止连接");
break; //断开连接的时候就停止无线循环
}
}
}
}
客户端:
主程序:
复制代码 代码如下:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.Tool.OperateMouse;
import com.Tool.ReceiveImages;
public class ReceiveBroderCast {
public ExecutorService exector;
public static String IP="202.216.60.9";
public static void main(String[] args)
{
new ReceiveBroderCast(IP);
}
public ReceiveBroderCast(String IP) {
exector = Executors.newFixedThreadPool(2);
exector.execute(new ReceiveImages(IP));
exector.execute(new OperateMouse(IP));
}
}
import java.util.concurrent.Executors;
import com.Tool.OperateMouse;
import com.Tool.ReceiveImages;
public class ReceiveBroderCast {
public ExecutorService exector;
public static String IP="202.216.60.9";
public static void main(String[] args)
{
new ReceiveBroderCast(IP);
}
public ReceiveBroderCast(String IP) {
exector = Executors.newFixedThreadPool(2);
exector.execute(new ReceiveImages(IP));
exector.execute(new OperateMouse(IP));
}
}
接收截图代码:
复制代码 代码如下:
/*
* ly 2014-11-20
* 该类用于接收教师端的屏幕信息,不包括鼠标
* 使用socket()
*/
public class ReceiveImages extends Thread{
public BorderInit frame ;
public Socket socket;
public String IP;
public static void main(String[] args){
new ReceiveImages("202.216.60.7").start();
}
public ReceiveImages(String IP)
{
frame=new BorderInit();
this.IP=IP;
}
public void run() {
while(frame.getFlag()){
System.out.println("已经连接"+(System.currentTimeMillis()/1000)%24%60+"秒");
try {
socket = new Socket(IP,8002);
DataInputStream ImgInput = new DataInputStream(socket.getInputStream());
ZipInputStream imgZip = new ZipInputStream(ImgInput);
Image img = null;
try{
imgZip.getNextEntry(); //到Zip文件流的开始处
img = ImageIO.read(imgZip); //按照字节读取Zip图片流里面的图片
frame.jlbImg.setIcon(new ImageIcon(img));
frame.validate();
}catch (IOException e) {e.printStackTrace();}
try{
imgZip.close();
} catch (IOException e) {
System.out.println("连接断开");
}
try {
TimeUnit.MILLISECONDS.sleep(50);// 接收图片间隔时间
} catch (InterruptedException ie) {
ie.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
socket.close();
} catch (IOException e) {}
}
}
}
}
class BorderInit extends JFrame
{
private static final long serialVersionUID = 1L;
public JLabel jlbImg;
private boolean flag;
public boolean getFlag(){
return this.flag;
}
public BorderInit()
{
this.flag=true;
this.jlbImg = new JLabel();
this.setTitle("远程监控--IP:" + "--主题:" );
this.setSize(400, 400);
//this.setUndecorated(true);
//this.setAlwaysOnTop(true); //始终在最前面
this.add(jlbImg);
this.setLocationRelativeTo(null);
this.setExtendedState(Frame.MAXIMIZED_BOTH);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setVisible(true);
this.validate();
//窗口关闭事件
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
flag=false;
BorderInit.this.dispose();
System.out.println("窗体关闭");
System.gc(); //垃圾回收
}
});
}
}
* ly 2014-11-20
* 该类用于接收教师端的屏幕信息,不包括鼠标
* 使用socket()
*/
public class ReceiveImages extends Thread{
public BorderInit frame ;
public Socket socket;
public String IP;
public static void main(String[] args){
new ReceiveImages("202.216.60.7").start();
}
public ReceiveImages(String IP)
{
frame=new BorderInit();
this.IP=IP;
}
public void run() {
while(frame.getFlag()){
System.out.println("已经连接"+(System.currentTimeMillis()/1000)%24%60+"秒");
try {
socket = new Socket(IP,8002);
DataInputStream ImgInput = new DataInputStream(socket.getInputStream());
ZipInputStream imgZip = new ZipInputStream(ImgInput);
Image img = null;
try{
imgZip.getNextEntry(); //到Zip文件流的开始处
img = ImageIO.read(imgZip); //按照字节读取Zip图片流里面的图片
frame.jlbImg.setIcon(new ImageIcon(img));
frame.validate();
}catch (IOException e) {e.printStackTrace();}
try{
imgZip.close();
} catch (IOException e) {
System.out.println("连接断开");
}
try {
TimeUnit.MILLISECONDS.sleep(50);// 接收图片间隔时间
} catch (InterruptedException ie) {
ie.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
socket.close();
} catch (IOException e) {}
}
}
}
}
class BorderInit extends JFrame
{
private static final long serialVersionUID = 1L;
public JLabel jlbImg;
private boolean flag;
public boolean getFlag(){
return this.flag;
}
public BorderInit()
{
this.flag=true;
this.jlbImg = new JLabel();
this.setTitle("远程监控--IP:" + "--主题:" );
this.setSize(400, 400);
//this.setUndecorated(true);
//this.setAlwaysOnTop(true); //始终在最前面
this.add(jlbImg);
this.setLocationRelativeTo(null);
this.setExtendedState(Frame.MAXIMIZED_BOTH);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setVisible(true);
this.validate();
//窗口关闭事件
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
flag=false;
BorderInit.this.dispose();
System.out.println("窗体关闭");
System.gc(); //垃圾回收
}
});
}
}
接收鼠标信息并控制鼠标移动:
复制代码 代码如下:
import java.awt.AWTException;
import java.awt.Robot;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import javax.swing.JOptionPane;
/*
* 学生端 控制鼠标和教师端一致
* 该类 负责接收鼠标的信息 并且用robot.mouseMove()函数控制鼠标移动
*/
public class OperateMouse extends Thread{
public static void main(String[] args)
{
new OperateMouse("202.116.60.7").start();
}
private Socket socket;
public String IP;
private int OPERATE_PORT = 8001;
private Robot robot;
public OperateMouse(String IP)
{
this.IP = IP;
}
public void run() {
while(true){
try {
socket = new Socket(IP,OPERATE_PORT);
robot = new Robot();
//获取鼠标移动的信息
DataInputStream dataIn = new DataInputStream(socket.getInputStream());
String info="";
int r;
while((r=dataIn.read()) != -1){
info +=""+(char)r; //把字节数组中所有元素都变为字符型
}
dataIn.close();
System.out.println("数据流断开"+info);
if(info!=null){
String s[] = info.trim().split(",");
if("Movemouse".equals(s[0].trim()));
{
if (s.length == 3) {
int x = Integer.parseInt(s[1].trim());
int y = Integer.parseInt(s[2].trim());
System.out.println("输出鼠标的信息"+x+" "+ y);
robot.mouseMove(x, y);
}
}
}
} catch (IOException e) {
System.out.println("已断开连接");
break;
} catch (AWTException e) {
e.printStackTrace();
}
}
}
}
import java.awt.Robot;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import javax.swing.JOptionPane;
/*
* 学生端 控制鼠标和教师端一致
* 该类 负责接收鼠标的信息 并且用robot.mouseMove()函数控制鼠标移动
*/
public class OperateMouse extends Thread{
public static void main(String[] args)
{
new OperateMouse("202.116.60.7").start();
}
private Socket socket;
public String IP;
private int OPERATE_PORT = 8001;
private Robot robot;
public OperateMouse(String IP)
{
this.IP = IP;
}
public void run() {
while(true){
try {
socket = new Socket(IP,OPERATE_PORT);
robot = new Robot();
//获取鼠标移动的信息
DataInputStream dataIn = new DataInputStream(socket.getInputStream());
String info="";
int r;
while((r=dataIn.read()) != -1){
info +=""+(char)r; //把字节数组中所有元素都变为字符型
}
dataIn.close();
System.out.println("数据流断开"+info);
if(info!=null){
String s[] = info.trim().split(",");
if("Movemouse".equals(s[0].trim()));
{
if (s.length == 3) {
int x = Integer.parseInt(s[1].trim());
int y = Integer.parseInt(s[2].trim());
System.out.println("输出鼠标的信息"+x+" "+ y);
robot.mouseMove(x, y);
}
}
}
} catch (IOException e) {
System.out.println("已断开连接");
break;
} catch (AWTException e) {
e.printStackTrace();
}
}
}
}
希望本文所述对大家的Java程序设计有所帮助。
加载全部内容