Java screen sharing applets and java screen applets

Source: Internet
Author: User
Tags home screen

Java screen sharing applets and java screen applets

Recently, I have been designing software and software engineering courses. I am working on a screen monitoring system for the lab. I will refer to the code of my predecessors. After I understand it, I want to convert my own code. This is the way beginners imitate it.

Speaking of the screen monitoring system, there are teacher disconnections and student terminals. The teacher end is the Server end, and the student end is the Client end. The interesting part of the system should be screen broadcasting and screen monitoring. It is relatively simple to call other functions such as sign-in, lock screen, and timed shutdown.

Screen broadcasting: In terms of function implementation, the instructor-side machine continuously captures screen information and sends it to each student's computer in the form of pictures, as a result, students can see the teacher's operations on the computer. This is called screen broadcasting.

The trouble is that there is no mouse information when screenshots are taken. However, there are two solutions:

① When sending a message, 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 his computer.

② Send the mouse coordinates of the instructor to the student's terminal, and move the mouse of the student's computer in real time based on the coordinate information. In fact, the control function is involved, so the student's end cannot move the mouse.

Screen monitoring is relatively tricky. In fact, this includes two functions: ① The teacher monitors the screens of all students' computers; ② the teacher controls the computers of a certain student; due to concurrency, every client needs to send the screen information to the instructor in real time, which is a little troublesome, but it can still be implemented.

The screen sharing function without mouse is implemented temporarily, which is relatively simple and needs to be improved. However, it can be used as a tool class for later integration.

First, the instructor Server:

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 screenshot disappears, multi-threaded implementation, does not contain mouse information, and No optimization is performed on each Client */publicclass 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 () ;}// constructor enabling socket connection Robot robot obtaining screen size public SendScreenImg (int SERVERPORT) {try {serverSocket = new ServerSocket (SERVERPORT); serverSocket. setSoTimeout (864000000); robot = new Robot ();} catch (Exception e) {e. printStackTrace ();} screen = Toolkit. getdefatooltoolkit (). getScreenSize (); // obtain the size of the home screen rect = new Rectangle (screen); // create a Rectangle of the screen size} @ Overridepublic void run () {// wait for receiving screenshots in real time while (true) {try {socket = serverSocket. accept (); System. out. println ("Student port 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 ("real-time Client connection");} catch (IOException ioe) {System. out. println ("disconnected");} finally {if (socket! = Null) {try {socket. close ();} catch (IOException e) {e. printStackTrace ();}}}}}}

 

Then the student Client:

Package Test; import java. awt. frame; import java. awt. image; import java. awt. event. windowAdapter; import java. awt. event. using wevent; 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 the screen information of the instructor, excluding the mouse. */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 (); // Image img = ImageIO at the beginning of the Zip file stream. read (imgZip); // read the image frame in the Zip image stream by byte. jlbImg. setIcon (new ImageIcon (img); System. out. println ("connection no." + (System. currentTimeMillis ()/1000) % 24% 60 + "seconds"); frame. validate (); TimeUnit. MILLISECONDS. sleep (50); // The interval between receiving images. imgZip. close ();} catch (IOException | InterruptedException e) {System. out. println ("disconnected");} finally {try {socket. close () ;}catch (IOException e) {}}}// Client-side window helper class, used to display the screen information received from the instructor. 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:" + "-- topic:"); this. setSize (400,400); // this. setUndecorated (true); // full screen display. It is best to comment out this during testing. setAlwaysOnTop (true); // the display window is always at the beginning of this. add (jlbImg); this. setLocationRelativeTo (null); this. setExtendedState (Frame. MAXIMIZED_BOTH); this. setdefaclocloseoperation (DISPOSE_ON_CLOSE); this. setVisible (true); this. validate (); // window close event this. addWindowListener (new WindowAdapter () {public void windowClosing (invalid wevent e) {flag = false; BorderInit. this. dispose (); System. out. println ("form closed"); System. gc (); // garbage collection }});}}

 

It's very late. I have extracted such a small feature from the unfinished product. Because I promise to give a link to a child shoes, there are still a lot to write about from the finished product. I will try it later.

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.