The example in this article describes how to implement the screen sharing function in Java. Share it with everyone for your reference. The specific analysis is as follows:
Recently I am designing a software engineering course, making a screen monitoring system for the laboratory, referring to various predecessor codes, and finally converting my own code after understanding it. This is how beginners imitate it.
When it comes to the screen monitoring system, there is a teacher side and a student side. The teacher side is the server side, and the student side is the client side. One of the more interesting aspects of the system is probably screen broadcasting and screen monitoring. The rest of the roll call, screen lock, and scheduled shutdown are relatively simple.
Screen broadcasting, in terms of function implementation, to put it bluntly, is that the teacher's machine continuously intercepts screen information and sends it to each student's computer in the form of pictures, so that students can see the teacher's operations on the computer. This is the so-called screen broadcast.
There is a troublesome thing here, that is, when taking a screenshot of the screen, there is no mouse information. But there are two solutions:
① When sending screenshot information, draw a mouse on the picture, so that there will be two mice on the student side, and the student side can move the mouse on their own computer.
②Send the mouse coordinates of the teacher's side to the student's side, and the student's computer mouse moves in real time according to the coordinate information. This actually involves the control function, and the student's side cannot move the mouse.
Screen monitoring is relatively tricky. In fact, it contains two functions:
①The teacher can monitor all students’ computer screens;
②The teacher controls a student’s computer;
Because it involves concurrency, each client must send screen information to the teacher in real time, which will be a bit troublesome, but it can still be achieved.
The screen sharing function without a mouse is temporarily implemented here. It is relatively simple and needs to be improved, but it can be used as a tool class for integration later.
The first is the teacher server:
Copy the code as follows: package Test;
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;
/*
* ly 2014-11-20
* This type of real-time sending of screenshots disappears, multi-threaded implementation, does not include mouse information, and does not optimize each Client.
*/
public class SendScreenImg extends Thread
{
public static int SERVERPORT=8000;
private ServerSocket serverSocket;
private Robot robot;
public dimension screen;
public Rectangle rect;
private Socket socket;
public static void main(String args[])
{
new SendScreenImg(SERVERPORT).start();
}
//Construction method to open the socket connection robot and get the screen size
public SendScreenImg(int SERVERPORT)
{
try {
serverSocket = new ServerSocket(SERVERPORT);
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 screen-sized rectangle
}
@Override
public void run()
{
//Waiting in real time to receive screenshot messages
while(true)
{
try{
socket = serverSocket.accept();
System.out.println("Student port is connected");
ZipOutputStream zip = new ZipOutputStream(new DataOutputStream(socket.getOutputStream()));
zip.setLevel(9); //Set the compression level
BufferedImage img = robot.createScreenCapture(rect);
zip.putNextEntry(new ZipEntry("test.jpg"));
ImageIO.write(img, "jpg", zip);
if(zip!=null)zip.close();
System.out.println("Client is connecting in real time");
} catch (IOException ioe) {
System.out.println("Connection disconnected");
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {e.printStackTrace();}
}
}
}
}
}
Then there is the student client:
Copy the code as follows: package Test;
import java.awt.Frame;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipInputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
/*
* ly 2014-11-20
* This class is used to receive screen information on the teacher's side, excluding the mouse, and needs to be optimized.
*/
public class ReceiveImages extends Thread{
public BorderInit frame;
public Socket socket;
public String IP;
public static void main(String[] args){
new ReceiveImages(new BorderInit(), "127.0.0.1").start();
}
public ReceiveImages(BorderInit frame,String IP)
{
this.frame = frame;
this.IP=IP;
}
public void run() {
while(frame.getFlag()){
try {
socket = new Socket(IP,8000);
DataInputStream ImgInput = new DataInputStream(socket.getInputStream());
ZipInputStream imgZip = new ZipInputStream(ImgInput);
imgZip.getNextEntry(); //Go to the beginning of the Zip file stream
Image img = ImageIO.read(imgZip); //Read the images in the Zip image stream according to bytes
frame.jlbImg.setIcon(new ImageIcon(img));
System.out.println("Connection number"+(System.currentTimeMillis()/1000)%24%60+"seconds");
frame.validate();
TimeUnit.MILLISECONDS.sleep(50);//Interval time for receiving pictures
imgZip.close();
} catch (IOException | InterruptedException e) {
System.out.println("Connection disconnected");
}finally{
try {
socket.close();
} catch (IOException e) {}
}
}
}
}
//Client side window auxiliary class, specially used to display screen information received from the teacher side
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); //Full screen display, it is best to comment out when testing
//this.setAlwaysOnTop(true); //The display window is always at the front
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
}
});
}
}
Here is such a small function extracted from the unfinished product. There is still a lot to write before the finished product. Interested friends can improve it on this basis.
I hope this article will be helpful to everyone’s Java programming.