hey guys m having some trouble with my project. the tutorials r helpful but not convenient enough for the time i have,sorry. i'm trying to make an application which captures the desktop, mouse events and the keyboard events. Its should also be able to send the message to the remote system and also be able to shutdown that remote system.
// this is the server-side coding import java.awt.AWTException; import java.awt.Image; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.ObjectOutputStream; import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import javax.swing.ImageIcon; import javax.swing.JOptionPane; import java.awt.event.*; public class JVNCserver { private final int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width; private final int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height; private final Rectangle screenRect = new Rectangle(0, 0, WIDTH, HEIGHT); public Socket socket; public ServerSocket s; private CaptureThread capturethread; private CaptureEvents captureevents; private Robot robot; private ObjectOutputStream out; private BufferedReader in; private BufferedImage i; Image image; public static void main(String arg[]) { JVNCserver s = new JVNCserver(); } public JVNCserver() { try { s = new ServerSocket(1166); socket = s.accept(); System.out.println("Server Started"); out = new ObjectOutputStream(socket.getOutputStream()); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); robot = new Robot(); capturethread = new CaptureThread(); captureevents = new CaptureEvents(); } catch (IOException ex) { ex.printStackTrace(); } catch(Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null,"server is stop"); } } public void sendImage() throws IOException { i = robot.createScreenCapture(screenRect); image = i.getScaledInstance(WIDTH, HEIGHT-60, Image.SCALE_SMOOTH); out.writeObject(new ImageIcon(image)); i.flush(); image.flush(); out.flush(); } private class CaptureThread implements Runnable { private volatile boolean keepRunning; Thread thread; CaptureThread() { thread = new Thread(this,"Capture Thread"); keepRunning = true; thread.start(); } public void run() { while (keepRunning) { try { sendImage(); Thread.currentThread().sleep(8000); //Thread.currentThread().sleep(20000); } catch(InterruptedException e1) { System.out.println("Thread Stop"); } catch (IOException ex) { ex.printStackTrace(); } catch(Exception e) { JOptionPane.showMessageDialog(null,"server is stoped"); } } } public void stopRunning() { keepRunning = false; } } private class CaptureEvents implements Runnable { private volatile boolean keepRunning; Thread thread; private int HY = HEIGHT / 2; int y; CaptureEvents() { thread = new Thread(this,"Capture Events"); keepRunning = true; thread.start(); } public void run() { while (keepRunning) { try { if (in!=null) { String e = (String)in.readLine(); if (e!=null) { //System.out.println(e); int eventType = Integer.parseInt(e.substring(0, e.indexOf("|"))); int arg1 = Integer.parseInt(e.substring(e.indexOf("|")+1, e.lastIndexOf("|"))); int arg2 = Integer.parseInt(e.substring(e.lastIndexOf("|")+1)); //System.out.println(arg1+"-"+arg2); if(eventType==JVNCconstant.CLOSE) { keepRunning = false; in.close(); out.close(); return; } if(arg2 < HY) y = arg2 - 20; else if(arg2 > HY) y = arg2 + 21; if (eventType == JVNCconstant.MOUSE_MOVE) robot.mouseMove(arg1,y); else if (eventType == JVNCconstant.MOUSE_PRESS) { robot.mousePress(InputEvent.BUTTON1_MASK); } else if (eventType==JVNCconstant.MOUSE_RELEASE) robot.mouseRelease(InputEvent.BUTTON1_MASK); else if(eventType== JVNCconstant.MOUSE_WHEEL) robot.mouseWheel(arg1); else if(eventType == JVNCconstant.KEY_PRESS_EVENTS) { switch(arg1) { case KeyEvent.VK_ENTER: robot.keyPress(KeyEvent.VK_ENTER); break; case KeyEvent.VK_F1: robot.keyPress(KeyEvent.VK_F1); break; case KeyEvent.VK_ESCAPE: robot.keyPress(KeyEvent.VK_ESCAPE); break; } } } } else System.out.println("In empty"); //Thread.currentThread().sleep(50); } /*catch(InterruptedException e) { System.out.println("Thread Stop"); }*/ catch(SocketException ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(null,"Client is stopped"); break; } catch (IOException ex) { ex.printStackTrace(); } catch(Exception e) { JOptionPane.showMessageDialog(null,"Server is stopped"); } } } public void stopRunning() { keepRunning = false; } } }//this is the client side programming 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; } } }//this is the coding for the RobotCheck function import java.awt.Robot; import java.awt.event.*; import java.awt.*; class RobotCheck { public static void main(String argv[]) { int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width; int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height; try { System.out.println(WIDTH +" " + HEIGHT); Robot robot = new Robot(); robot.mousePress(InputEvent.BUTTON1_MASK); System.out.println(KeyEvent.VK_ENTER); System.out.println(KeyEvent.VK_F1); //robot.keyPress(KeyEvent.VK_ENTER); //robot.mouseMove(12,555); } catch(Exception e) { } } }hope some1 can help me on creating a user-interface for this application with the connectivity to buttons such as "CONNECT", "SEND MESSAGE", "KILL SYSTEM","DISCONNECT"//this is the coding for the constant function public class JVNCconstant { public static final int CLOSE = 0; public static final int MOUSE_MOVE = 1; public static final int MOUSE_PRESS = 2; public static final int MOUSE_RELEASE = 3; public static final int MOUSE_WHEEL = 4; public static final int KEY_PRESS = 6; public static final int KEY_RELEASE = 7; public static final int KEY_TYPED = 5000; public static final int KEY_PRESS_EVENTS = 12345; }
any help will be much appreciated...