Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline
Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Subscription to Java Pro

JVNC Gives You Robotic Control (Continued)

To speed up the connection, a certain amount of compression of the image is performed. It would be far too slow to send an entire image of a desktop that has a 1400x1050 resolution with a 32-bit color scheme, especially in the normal BufferedImage object that the Robot's createScreenCapture() method returns. In sacrificing image quality for an increase in speed of transmission, an effort was made to make the Object sent over the connection smaller. These compression steps reduce the image from a 1400x1050-pixel image (4.09 MB) to a 700x525-pixel image (1.02 MB). The now shrunken image is wrapped in an ImageIcon (because BufferedImage is not serializable) and sent back to the client:

robot = new Robot();
Image i = 
  robot.createScreenCapture(
  screenRect).getScaledInstance(
  WIDTH/2, HEIGHT/2, 
  Image.SCALE_SMOOTH);
ImageIcon icon = 
  new ImageIcon(i);
out.writeObject(icon);
out.flush();

The final step in the RobotThread() creation is to spawn another new thread: the CaptureThread(). To smoothly capture all the client messages, as well as smoothly return screen shots, JVNC has both the RobotThread capturing client messages and the CaptureThread() sending the screen shot back to the client:

CaptureThread t = 
  new CaptureThread(robot, out);
Thread thread = new Thread(t);
thread.start();
ADVERTISEMENT

The RobotThread()'s run method's sole responsibility is to read the messages sent by the clients and convert them to commands for the server's Robot. There are six Robot methods that manipulate the OS. Each of these methods has a matching constant in the JVNCConstants class that both the client and server use for consistency during communication. In addition, all the methods take either one or two integer arguments, and those are passed from the client as well. In mouse events, these integer arguments are the mouse button pressed or the x,y coordinates. In keyboard events, these integer arguments are the keys pressed. The message from the client, in the form of eventType|intArg1|intArg2, is parsed and executed by the Robot:

if (eventType == 
  JVNCConstants.CLOSE)
  return;
else if (eventType == 
  JVNCConstants.KEY_PRESS)
  robot.keyPress(intArg1);
else if (eventType == 
  JVNCConstants.KEY_RELEASE)
  robot.keyRelease(intArg1);
else if (eventType == 
  JVNCConstants.MOUSE_MOVE)
  robot.mouseMove(
    intArg1, intArg2);
else if (eventType == 
  JVNCConstants.MOUSE_PRESS)
  robot.mousePress(intArg1);
else if (eventType == 
  JVNCConstants.MOUSE_RELEASE)
  robot.mouseRelease(intArg1);
else if (eventType == 
  JVNCConstants.MOUSE_WHEEL)
  robot.mouseWheel(intArg1);

In the same technique in which the RobotThread() sent the initial screen shot back to the client, CaptureThread() simply sends screen shots back to the client continually as feedback to the messages the client sends to be interpreted by the Robot.




Back to top













Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home