The example in this article describes how Java can implement a screen broadcast by controlling the mouse. Share to everyone for your reference. The specific analysis is as follows:
In the previous "Java implementation screen sharing function example analysis" mentioned in the interception screen is not a mouse, in order to see the teacher-side interface of the mouse, you can intercept the screen, the mouse to draw to each screenshot, but because the screenshot is a piece of the interception, so see the mouse will inevitably be a little card, Previously written Java Mouse Control applet, you can see the mouse in this way to demonstrate.
Implementation of the way is also quite simple, the previous two articles to achieve the mouse control and without the mouse screen sharing function, the two combine the OK, the following simple analysis.
Service side, the sendscreenimg and Sendmousemessage as two tool classes, respectively listening to different ports, they both implemented the thread class, we use thread pool Executorservice class control them.
Two ports are used because we don't know what to do with the information of the mouse and the pictures. Perhaps the image can be converted into a byte array, the mouse coordinates placed in front of the array, but in this case the mouse may also be inconsistent, because the speed of the transfer of the mouse coordinates than the transfer of pictures faster, um, Try again when you are free.
The client analogy is above it.
Here's the code:
Service side:
Main program:
Copy Code code as follows:
/*
* Screen broadcast class, called Two tool classes: send screenshots of information classes and send the mouse information class, leveraging 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 Code 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;
/*
* Tool class: Send the Teacher end screen information to the students, no mouse information, the use of Port No. No. 8002
* Can be sent in the picture above the component to draw the mouse information, so as to achieve in the student interface to see the mouse information, temporarily did not implement the function
*
*/
public class Sendscreenimg extends Thread
{
public int serverport=8002;
Private ServerSocket ServerSocket;
Private Robot Robot;
public Dimension screens;
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) {}
}
Construct method to open socket connection robot robot get 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 home screen
Rect = new Rectangle (screen); To construct a rectangle of the appropriate size
}
@Override
public void Run ()
{
Waiting for a screenshot message in real time
while (true) {
try {
Socket = Serversocket.accept ();
Zipoutputstream zip = new Zipoutputstream (New DataOutputStream (Socket.getoutputstream ()));
Zip.setlevel (9); Set 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 already connected");
catch (IOException IoE) {
SYSTEM.OUT.PRINTLN ("Controlled End: Disconnect");
}
catch (IOException IoE) {
SYSTEM.OUT.PRINTLN ("Connection error");
finally {
if (socket!= null) {
try {
Socket.close ();
catch (IOException e) {
}
}
}
}
}
}
Send mouse information:
Copy Code code as follows:
/*
* Tool class: Get the mouse information and send it to the student side
*/
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, "started listening");
catch (IOException E1) {
E1.printstacktrace ();
}
}
Multithreading listens to 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 (); Brush the output stream, and make all buffered output bytes write out
Output.close (); Turn off the output stream and release the resource
System.out.println ("INFO:" +operatestr);
catch (IOException e) {
System.out.println ("has ceased to connect");
Break Stop the wireless loop when you disconnect
}
}
}
}
Client:
Main program:
Copy Code 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 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 ();
}
Public receiveimages (String IP)
{
Frame=new Borderinit ();
This. Ip=ip;
}
public void Run () {
while (Frame.getflag ()) {
System.out.println ("Already 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 (); At the beginning of the zip file stream
img = Imageio.read (imgzip); Read the picture in the zip picture stream by byte
Frame.jlbImg.setIcon (New ImageIcon (IMG));
Frame.validate ();
}catch (IOException e) {e.printstacktrace ();}
try{
Imgzip.close ();
catch (IOException e) {
System.out.println ("Disconnected");
}
try {
TimeUnit.MILLISECONDS.sleep (50);//Receive Picture time interval
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 ("Remote monitoring--ip:" + "--Subject:");
This.setsize (400, 400);
This.setundecorated (TRUE);
This.setalwaysontop (TRUE); 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 shutdown events
This.addwindowlistener (New Windowadapter () {
public void windowclosing (WindowEvent e) {
Flag=false;
BorderInit.this.dispose ();
System.out.println ("form close");
System.GC (); Garbage collection
}
});
}
}
Receive mouse information and Control mouse movement:
Copy Code 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;
/*
* Student-side Control mouse and instructor-side consistency
* This class is responsible for receiving information about the mouse and using the Robot.mousemove () function to control mouse movement
*/
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 information about the mouse movement
DataInputStream DataIn = new DataInputStream (Socket.getinputstream ());
String info= "";
int R;
while ((R=datain.read ())!=-1) {
info + = "" + (char) r; Changes all elements in a byte array to a character type
}
Datain.close ();
SYSTEM.OUT.PRINTLN ("Data stream 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 help you with your Java programming.