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)

Client JFrame
The client-side code is fairly straightforward. Remember the client's goal is simply to send mouse and keyboard events to the server. With that in mind, the client creates a JFrame, which contains one JLabel inside of it that will be used to display the screen shots sent by the server. The most important note is that the client application adds itself as a MouseListener, MouseMotionListener, MouseWheelListener, and KeyListener to the frame, so that it can capture any input that is generated on that frame (mouse presses, movements, and key presses) and send that message to the server side. Finally, it creates I/O links with the server, captures the initial image sent by the server, and displays the frame:

f = new JVNCFrame();
f.addKeyListener(this);
f.getLabel().addMouseListener(
  this);
f.getLabel().
  addMouseMotionListener(this);
f.getLabel().
  addMouseWheelListener(this);
try
{
  Socket s = new Socket(
    serverName, 1166);
  out = new PrintWriter(
    s.getOutputStream());
  in = new ObjectInputStream(
    s.getInputStream());

ImageIcon i = (
  ImageIcon)in.readObject();
  f.setSize(i.getIconWidth()+40, 
    i.getIconHeight()+40);
  f.updateScreen(i);

  f.setVisible(true);
}

Like the server-side code, another thread is spawned with the sole purpose of listening on the port for new images from the server. When an image is received, it updates the JLabel that exists on the client application frame:

ImageIcon i = (
  ImageIcon)in.readObject();
f.updateScreen(i);

Finally, as an example of how the client receives messages and sends them to the server, this example shows how the client handles a mouse press:

public void mousePressed(
  MouseEvent e)
{ 
int mouse = 0;
  if (SwingUtilities.
    isLeftMouseButton(e))
    mouse = 
      InputEvent.BUTTON1_MASK;
  if (SwingUtilities.
    isMiddleMouseButton(e))
    mouse = 
      InputEvent.BUTTON2_MASK;
  else if (SwingUtilities.
    isRightMouseButton(e))
    mouse = InputEvent.
      BUTTON3_MASK;
  String s = "" + 
    JVNCConstants.MOUSE_PRESS + 
    "|" + mouse + "|" + 0;
  out.println(s);
  out.flush();
}
ADVERTISEMENT

What, then, is the answer to the question "What the heck do you use the java.awt.Robot class for?" By analyzing the sample application (JVNC), and even better, by using JVNC amongst your computers (although this version isn't optimized for usage in the field, it is instead presented for education), you will see the functionality that the Robot class can provide. In terms of help desks, customer support, IT support, and remote testing, the Robot class is invaluable.

As a quick example of the Robot in action, imagine an application that is being ported to five different OS's (Windows, Macintosh, and three flavors of Unix) and the tester that is responsible for regression testing them on each machine. He or she could go to all five machines and run the tests, or instead could sit at their own machine with five windows open running the tests. This scenario demonstrates the power of the Robot. Now, after seeing the usefulness of it, I hope that when you hear about the java.awt.Robot, you think "I know what the Robot can do" (a few thousands of hits on Google).

About the Authors
Michael Abernethy works with the IBM WebSphere System Management team based in Austin, Texas and provides WebSphere solutions to an on-demand world. Contact Mike at .

Kulvir Singh Bhogal is an IBM Software Services consultant for WebSphere, implementing IBM's e-business strategies across the U.S. Contact Kulvir at .




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