//the main program is as follows
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.*;
import javax.swing.JPanel;
import java.awt.event.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class JVNCclient extends JFrame implements MouseWheelListener
{
private JLabel screen;
private ObjectInputStream in;
private PrintWriter out;
private ScreenThread screenThread;
private ImageIcon i;
private Socket s;
private boolean stopped = false;
public JVNCclient()
{
int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
setTitle("Remote Desktopcapture");
setBackground(Color.white);
screen = new JLabel();
getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
getContentPane().add(screen);
resize(WIDTH,HEIGHT-10);
addMouseListener(new MyMouseAdapter(this));
addMouseMotionListener(new MyMouseMotionAdapter(this));
addKeyListener(new MyKeyAdapter(this));
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
stopped = true;
screenThread.stopRunning();
sendAndUpdate(JVNCconstant.CLOSE,0,0);
System.exit(0);
}
});
String serverName = JOptionPane.showInputDialog(this, "Enter Server name :", "Server", JOptionPane.INFORMATION_MESSAGE);
try
{
s = new Socket(serverName.trim(), 1166);
if (s==null)
{
System.out.println("No I/O");
System.exit(0);
}
else
{
in = new ObjectInputStream(s.getInputStream());
out = new PrintWriter(s.getOutputStream());
screenThread = new ScreenThread();
}
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null,"Server not found on PORT number 1166");
System.exit(0);
}
}
public void updateScreen(final ImageIcon i)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
screen.repaint();
screen.setIcon(i);
}
});
}
private void sendAndUpdate(int type,int arg1, int arg2)
{
String s = "" + type + "|" + arg1 + "|" + arg2;
out.println(s);
out.flush();
}
class MyMouseAdapter extends MouseAdapter
{
JVNCclient jvnc;
public MyMouseAdapter(JVNCclient jvnc)
{
this.jvnc = jvnc;
}
public void mousePressed(MouseEvent e)
{
sendAndUpdate(JVNCconstant.MOUSE_PRESS, e.getX(),e.getY());
}
public void mouseReleased(MouseEvent e)
{
sendAndUpdate(JVNCconstant.MOUSE_RELEASE,0,0);
}
}
class MyMouseMotionAdapter extends MouseMotionAdapter
{
JVNCclient jvnc;
public MyMouseMotionAdapter(JVNCclient jvnc)
{
this.jvnc = jvnc;
}
public void mouseDragged(MouseEvent e)
{
sendAndUpdate(JVNCconstant.MOUSE_MOVE, e.getX(), e.getY());
}
public void mouseMoved(MouseEvent e)
{
sendAndUpdate(JVNCconstant.MOUSE_MOVE,e.getX(), e.getY());
}
}
public void mouseWheelMoved(MouseWheelEvent e)
{
sendAndUpdate(JVNCconstant.MOUSE_WHEEL,e.getScrollAmount(),0);
}
class MyKeyAdapter extends KeyAdapter
{
JVNCclient jvnc;
public MyKeyAdapter(JVNCclient jvnc)
{
this.jvnc = jvnc;
}
public void keyPressed(KeyEvent ke)
{
int key;
key = ke.getKeyCode();
if(key==ke.VK_ENTER)
sendAndUpdate(JVNCconstant.KEY_PRESS_EVENTS,ke.VK_ENTER,0);
else if(key==ke.VK_F1)
sendAndUpdate(JVNCconstant.KEY_PRESS_EVENTS,ke.VK_F1,0);
else if(key==ke.VK_ESCAPE)
sendAndUpdate(JVNCconstant.KEY_PRESS_EVENTS,ke.VK_ESCAPE,0);
}
}
public static void main(String argv[])
{
JVNCclient f = new JVNCclient();
f.setVisible(true);
f.show();
}
private class ScreenThread implements Runnable
{
Thread t;
private boolean keepRunning;
ScreenThread()
{
keepRunning = true;
t = new Thread(this,"Screen Thread");
t.start();
}
public void run()
{
while (keepRunning)
{
try
{
if(stopped ==false)
{
if((i = (ImageIcon)in.readObject())!=null)
{
updateScreen(i);
Thread.sleep(1000);
i=null;
}
}
else
{
keepRunning = false;
in.close();
}
}
catch(InterruptedException e)
{
System.out.println("Thread Interrupted");
}
catch(OutOfMemoryError e)
{
e.toString();
JOptionPane.showMessageDialog(null,"JVM can not able to allocate Memory");
System.exit(0);
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
catch (Exception ex)
{
ex.printStackTrace();
JOptionPane.showMessageDialog(null,"Server is stoped");
System.exit(0);
}
}
}
public void stopRunning()
{
keepRunning = false;
}
}
}