Java mouse control applet, java Control Applet

Source: Internet
Author: User

Java mouse control applet, java Control Applet

Recently, I am working on a softwarehouse screen monitoring software, which has realized the transmission of screen images. However, without the mouse, I found that the PtrScSysRq key on the keyboard has no mouse information. =

Currently, you only need to move the mouse to the event and use the robot. mouseMove (x, y) function to implement the event. Therefore, the MouseEvent object is not used and the getPointerInfo () method in the MouseInfo class is used.

You need to know that in Swing programs, the mouse coordinates are usually obtained through the MouseEvent object of the mouse event. In this case, you can only get the coordinates of the mouse in the form event (refer to Java to get the mouse position through MouseInfo)

Programs can be divided into control terminals and controlled terminals, and run on two PCs.

 

Control End

Import java. awt. mouseInfo; import java. awt. point; import java. io. dataOutputStream; import java. io. IOException; import java.net. serverSocket; import java.net. socket; // The instructor sends the mouse information to the student Port 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, "listener started");} catch (IOException e1) {e1.printStackTrace () ;}// multithreading listens to the public void run () of the client in a wireless Loop () {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 all buffered output bytes. close (); // close the output stream and release the resource System. out. println ("INFO:" + operateStr);} catch (IOException e) {System. out. println ("stopped connections"); break; // stop the wireless loop when the connection is disconnected}/* try {Thread. sleep (1, 100); System. out. println ("stop 100 microseconds");} catch (InterruptedException e) {e. printStackTrace ();}*/}}}

 

Controlled Terminal

Import java. awt. AWTException; import java. awt. robot; import java. io. dataInputStream; import java. io. IOException; import java.net. socket;/** the control mouse on the student side is the same as that on the instructor side * this class is responsible for receiving mouse information and using robot. the mouseMove () function controls mouse movement */public class OperateMouse implements Runnable {public static void main (String [] args) {new Thread (new OperateMouse ()). start ();} private Socket socket; private int OPERATE_PORT = 8001; private Robot robot; @ Ove Rridepublic void run () {while (true) {try {socket = new Socket ("202.116.60.6", OPERATE_PORT); robot = new Robot (); DataInputStream dataIn = new DataInputStream (socket. getInputStream (); String info = ""; int r; while (r = dataIn. read ())! =-1) {info + = "" + (char) r; // changes all elements in the byte array to bytes type. // System. out. println ("current data read" + info);} 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) {// TODO Auto-generated catch blocke. printStackTrace ();}}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.