The example in this article describes how Java implements screen broadcasting by controlling the mouse. Share it with everyone for your reference. The specific analysis is as follows:
In the previous article "Analysis of Examples of Implementing Screen Sharing Function in Java", it was mentioned that there is no mouse when capturing the screen. In order to see the mouse on the teacher interface, you can draw the mouse to each screenshot when capturing the screen. However, since the screenshots are taken one by one, the mouse you see will inevitably be a bit stuck. I wrote a Java mouse control applet before, and you can see the mouse demonstration in this way.
The implementation method is also quite simple. The previous two articles implemented mouse control and screen sharing functions without a mouse respectively. It is ok to combine the two. Let’s briefly analyze it below.
On the server side, SendScreenImg and SendMouseMessage are regarded as two tool classes, listening to different ports respectively. Both of them implement the Thread class, and we use the thread pool ExecutorService class to control them.
Two ports are used, because I don’t know yet how to send the mouse information and the picture information together. Maybe I can convert the picture into a byte array and put the mouse coordinates in front of the array, but in this case the mouse may not It will be incoherent, because the speed of transmitting mouse coordinates will be faster than transmitting pictures. Well, try again when you have time.
The client analogy is the same as above.
Here is the code:
Server:
Main program:
Copy the code code as follows:/*
* The screen broadcast class calls two tool classes: a class for sending screenshot information and a class for sending mouse information, using the thread pool.
*/
public class BroderCast {
public static void main(String[] args)
{
new BroderCast();
System.out.println("Start");
}
public BroderCast()
{
ExecutorService exector = Executors.newFixedThreadPool(2);
exector.execute(new SendScreenImg());
exector.execute(new SendMouseMessage());
}
}
Send screenshot code:
Copy the code as follows: 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;
/*
* Tools: Send screenshot information from the teacher side to the student side. There is no mouse information and port 8002 is used.
* The mouse information can be drawn on the component of the sent picture, so that the mouse information can be seen on the student interface. This function has not been implemented yet.
*
*/
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(); //It is necessary to close the current port first
this.serverPort = serverPort;
serverSocket = new ServerSocket(this.serverPort);
serverSocket.setSoTimeout(8000000);
}catch(Exception e){}
}
//Construction method to open the socket connection robot and get the screen size
public SendScreenImg()
{
try {
serverSocket = new ServerSocket(getServerPort());
serverSocket.setSoTimeout(864000000);
robot = new Robot();
} catch (Exception e) {
e.printStackTrace();
}
screen = Toolkit.getDefaultToolkit().getScreenSize(); //Get the size of the main screen
rect = new Rectangle(screen); //Construct a rectangle of corresponding size
}
@Override
public void run()
{
//Waiting in real time to receive screenshot messages
while(true){
try {
socket = serverSocket.accept();
ZipOutputStream zip = new ZipOutputStream(new DataOutputStream(socket.getOutputStream()));
zip.setLevel(9); //Set the compression level
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("Student port is connected");
} catch (IOException ioe) {
System.out.println("controlled terminal: disconnect");
}
} catch (IOException ioe) {
System.out.println("Connection error");
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
}
}
}
Send mouse information:
Copy the code code as follows:/*
* Tool class: obtain mouse information and send it to the student terminal
*/
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, "has started listening");
} catch (IOException e1) {
e1.printStackTrace();
}
}
//Multiple threads monitor the client in a wireless loop
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(); //Flush the output stream and write out all buffered output bytes
output.close(); //Close the output stream and release resources
System.out.println("INFO: "+operateStr);
} catch (IOException e) {
System.out.println("Connection has stopped");
break; //Stop the wireless loop when disconnecting
}
}
}
}
Client:
Main program:
Copy the code as follows: 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));
}
}
Receive screenshot code:
Copy the code code as follows:/*
* ly 2014-11-20
* This class is used to receive screen information from the teacher side, excluding the mouse
* Use 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();
}
publicReceiveImages(String IP)
{
frame=new BorderInit();
this.IP=IP;
}
public void run() {
while(frame.getFlag()){
System.out.println("Connected"+(System.currentTimeMillis()/1000)%24%60+"seconds");
try {
socket = new Socket(IP,8002);
DataInputStream ImgInput = new DataInputStream(socket.getInputStream());
ZipInputStream imgZip = new ZipInputStream(ImgInput);
Image img = null;
try{
imgZip.getNextEntry(); //Go to the beginning of the Zip file stream
img = ImageIO.read(imgZip); //Read the images in the Zip image stream according to bytes
frame.jlbImg.setIcon(new ImageIcon(img));
frame.validate();
}catch (IOException e) {e.printStackTrace();}
try{
imgZip.close();
} catch (IOException e) {
System.out.println("Connection disconnected");
}
try {
TimeUnit.MILLISECONDS.sleep(50);//Interval time for receiving pictures
} 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;
}
publicBorderInit()
{
this.flag=true;
this.jlbImg = new JLabel();
this.setTitle("Remote monitoring--IP:" + "--Topic:" );
this.setSize(400, 400);
//this.setUndecorated(true);
//this.setAlwaysOnTop(true); //Always on top
this.add(jlbImg);
this.setLocationRelativeTo(null);
this.setExtendedState(Frame.MAXIMIZED_BOTH);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setVisible(true);
this.validate();
//window close event
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
flag=false;
BorderInit.this.dispose();
System.out.println("Form closed");
System.gc(); //garbage collection
}
});
}
}
Receive mouse information and control mouse movement:
Copy the code as follows: 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;
/*
* The mouse control on the student side is the same as that on the teacher side
* This class is responsible for receiving mouse information and controlling mouse movement using the robot.mouseMove() function
*/
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();
//Get mouse movement information
DataInputStream dataIn = new DataInputStream(socket.getInputStream());
String info="";
int r;
while((r=dataIn.read()) != -1){
info +=""+(char)r; //Convert all elements in the byte array to character type
}
dataIn.close();
System.out.println("Data flow disconnected"+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("Output mouse information"+x+" "+ y);
robot.mouseMove(x, y);
}
}
}
} catch (IOException e) {
System.out.println("Disconnected");
break;
} catch (AWTException e) {
e.printStackTrace();
}
}
}
}
I hope this article will be helpful to everyone’s Java programming.