Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

Cool Java Programs

4.14/5 (5 votes)
18 Jul 2011CPOL2 min read 100K   1.8K  
These are Java Programs for typing on computer without physically typing, screen capture, AutoDownloader, etc.

Introduction

This includes Java Programs for:

  1. Typing on computer without physically typing
  2. Screen capture
  3. Auto Downloader
  4. Listing all processes in task manager
  5. Obtaining and setting Clipboard data

Using the Code

1) Typing on Computer without Physically Typing

The first program opens an editor like Notepad where our program will start typing automatically. This is achieved by:

Java
Runtime.getRuntime().exec("notepad.exe");

Now we make our program type in Notepad. For this, we use the Robot class:

Java
Thread.sleep(2000);
Robot r=new Robot();
r.keyPress(KeyEvent.VK_T);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_H);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_I);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_S);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_SPACE);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_I);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_S);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_SPACE);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_F);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_U);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_N);	

A starting delay of 2 secs is introduced so that Notepad gets a chance to open and our program writes in Notepad and not anywhere else.

After each letter, we introduced a delay of .5 sec so that it looks like someone is typing.

2) Screen Capture

For this also, we will make use of the Robot class:

Java
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(size));
File save_path=new File("screen.jpg");
ImageIO.write(img, "JPG", save_path);	 

Here Dimension gets your screen size and puts dimension in the size variable. createScreenCapture function takes a screenshot. ImageIO.write is responsible for writing the screenshot as a JPG file.

3) Auto Downloader

Here is a simple Java program to download a file with Java:

Java
System.out.println("Enter the url to download from:");
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String site = input.readLine();
System.out.println("Enter filename");
BufferedReader input1 = new BufferedReader(new InputStreamReader(System.in));
String filename = input.readLine();
java.io.BufferedInputStream in = new java.io.BufferedInputStream(new 	 
java.net.URL(site).openStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream(filename);
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
byte[] data = new byte[1024];
int i=0;
while((i=in.read(data,0,1024))>=0)
{
	bout.write(data,0,i);
}	

Here, first you need to give the URL of file to download. Now you have to specify the name by which it will be saved like a .zip. Here in variable is used to read the data obtained from URL and then this data is written to your system with the help of bout.

4) Task Manager Processes

It is very easy to list all processes that are active in task manager. Just use this program:

Java
String line;
Process p = Runtime.getRuntime().exec("tasklist.exe");
BufferedReader input =
                new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
	System.out.println(line); //<-- Parse data here.
}
input.close();

Here tasklist.exe gives us all the running processes. We store all results inside a variable input from which we later list the result to output console.

5) ClipBoard Details

It is very easy to get and set clipboard data. Here is a sample:

Java
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
	String text = (String)t.getTransferData(DataFlavor.stringFlavor);
	System.out.println("Current ClipBoard data is:\n"+text);
	text="I changed clipboard text";
	StringSelection ss = new StringSelection(text);
	Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
	System.out.println("Changed ClipBoard data is:\n"+text);
}	

This program first retrieves the current clipboard content, shows it and then changes it.

Points of Interest

The Robot class may also be used to get pixel information and can even simulate mouse click.

The Runtime.getRuntime.exec() gives the power to Java to execute Windows application directly.

The clipboard program can be enhanced further which can help you make a good desktop app.

History

  • 17th July, 2011: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)