Showing posts with label belazy blog. Show all posts
Showing posts with label belazy blog. Show all posts

April 14, 2015

Automatically typing notepad in java


A Notepad that works automatically in java


/**
 * Funny notepad automatically wishing birthday
 */
package com.blogspot.javabelazy.funny;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.lang.reflect.Field;

/**
 * @author +Vipin Cp
 *
 */
public class HappyBirthdayMessage {

/**
* @param tokyo
*/
public static void main(String[] tokyo) {
// TODO Auto-generated method stub
HappyBirthdayMessage message = new HappyBirthdayMessage();
String yourMessage = "Happy Birthday Jerin V George";
Runtime runtime = Runtime.getRuntime();
try {
                       Thread.sleep(5000);
runtime.exec("notepad");
Thread.sleep(3000);
message.createMessage(yourMessage);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

private void createMessage(String message) {
// TODO Auto-generated method stub
try {
Robot robot = new Robot();
int length = message.length();
for(int i=0;i<length;i++){
char letter = message.charAt(i);
String keyVal = Character.toString(letter);
String variableName;
if(keyVal.equals(" ")){
variableName ="VK_SPACE";
}else{
variableName = "VK_"+keyVal.toUpperCase();
}


Class clazz = KeyEvent.class;
   Field field = clazz.getField(variableName);
   int keyCode = field.getInt(null);
   robot.keyPress(KeyEvent.VK_SHIFT);
   robot.keyPress(keyCode);
   robot.delay(1000);
}

} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

Java tips and trick by +belazy

http://javabelazy.blogspot.in/

October 15, 2013

Sending screenshot from client to Server through socket in java


How to send screen shot from client to server automatically in java

Did you ever think of getting screen shot from all computers in a network to a server. This code will helps you to do so....This code will capture the current desktop screen shot and sent it to server computer. This application is very useful to network administrator to track what other are doing in the computer at your organization.

ClientApp.java : is a client program that takes screen shot in regular interval.  The sendScreen function will help you to know how java program takes screen shot at intervals and sends to server machine. Here i used ImageIO class to create image buffer. There are other options too... The file is sent through a socket. the port number i used here is 1729. Server Name here used is loop back ip, where you need to specify the server computer name or ip  address.

On the Other hand
SeverApp.java will always listen to the port 1729. if any request comes there, it will catch request and save to the folder 


ClientApp.java


import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;


import javax.imageio.ImageIO;


/**
 * @author lou reed
 *
 */
public class ClientApp implements Runnable {

    private static long nextTime = 0;
    private static ClientApp clientApp = null;
    private String serverName = "127.0.0.1"; //loop back ip
    private int portNo = 1729;
    //private Socket serverSocket = null;
   
    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        clientApp = new ClientApp();
        clientApp.getNextFreq();
        Thread thread = new Thread(clientApp);
        thread.start();
    }

    private void getNextFreq() {
        long currentTime = System.currentTimeMillis();
        Random random = new Random();
        long value = random.nextInt(180000); //1800000
        nextTime = currentTime + value;
        //return currentTime+value;
    }

    @Override
    public void run() {
        while(true){
            if(nextTime < System.currentTimeMillis()){
                System.out.println(" get screen shot ");
                try {
                    clientApp.sendScreen();
                    clientApp.getNextFreq();
                } catch (AWTException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch(Exception e){
                    e.printStackTrace();
                }
               
            }
            //System.out.println(" statrted ....");
        }
       
    }

    private void sendScreen()throws AWTException, IOException {
           Socket serverSocket = new Socket(serverName, portNo);
             Toolkit toolkit = Toolkit.getDefaultToolkit();
             Dimension dimensions = toolkit.getScreenSize();
                 Robot robot = new Robot();  // Robot class
                 BufferedImage screenshot = robot.createScreenCapture(new Rectangle(dimensions));
                 ImageIO.write(screenshot,"png",serverSocket.getOutputStream());
                 serverSocket.close();
    }
}




Facebook comments