0

I am trying to make a separate class for the Java AWT Robot to use with projects but i am having trouble setting it up how i would like as all of the examples I have found online seem to pack the code into a single .java file instead.

My code works fine however I am wondering if I could setup the functions in a nicer way.

The code for the RobotLib.java class is as follows:

package com.z;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

    public class RobotLib {

    private static Robot robot;

    // Press Function
    public void Press(int key, int time){ 
        try {
        Robot robot = new Robot();
            robot.keyPress(key);
            robot.delay(time);
            robot.keyRelease(key);

        } catch (AWTException e) {
            e.printStackTrace();
        }
    } 

}

And my Example.java code is:

package com.z;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Example {

    public static void main(String[] args) {

        RobotLib robot = new RobotLib();

        robot.Press(KeyEvent.VK_A,100); // type a

    }
}

With the RobotLib.java class I was wondering if it's possible to have the functions without wrapping them with try/catch and new Robot() so instead of the above version it would be something like this instead:

public void Press(int key, int time){ 
    robot.keyPress(key);
    robot.delay(time);
    robot.keyRelease(key);
}

The try/catch and new Robot() seem to be required however and if I take those away I get errors like this:

Exception in thread "main" java.lang.NullPointerException
    at com.z.RobotLib.Press(RobotLib.java:35)
    at com.z.Example.main(Example.java:14)

I am quite new to Java coding and might be setting up the class the wrong way, is there a way to fix that error or have the functions how I want?

zeddex
  • 1,260
  • 5
  • 21
  • 38

3 Answers3

1

Yes you do need the try/catch block in there, but yes there is also a way to set up those functions better. You don't need to create a robot each time you call the Press method. Create your static Robot instance in your constructor.

public class RobotLib {

private static Robot robot;

public RobotLib(){
  robot = new Robot();
}

// Press Function
public void Press(int key, int time){ 
    try {
        robot.keyPress(key);
        robot.delay(time);
        robot.keyRelease(key);

    } catch (AWTException e) {
        e.printStackTrace();
    }
} 

}
Raskolnikov
  • 286
  • 3
  • 13
  • Thanks, i am trying to avoid using the need of the try, catch and new Robot() though. Basically i wanted to shorten the functions and make them similar to the examples you can find online for the robot class where they use a single .java file. Like how i posted near to the end above the error message, is that possible in a class? – zeddex Aug 01 '12 at 01:25
  • I think i will just set it up how you suggest so i have accepted your answer, it would have been nice to have been able to avoid the try catch for each function but i guess that is not possible in a class. Thanks for the help. – zeddex Aug 01 '12 at 01:38
  • Actually having tested your code it is giving me errors. new Robot() and AWTException are highlighted in eclipse. Any idea how to correct this? – zeddex Aug 01 '12 at 01:50
  • 1
    You need to make sure you have AWTException imported properly. Also I assumed that the Robot class had an empty constructor, but so does the code above. What kind of error is it reporting? – Raskolnikov Aug 01 '12 at 02:22
  • I am not sure how to import the AWTException like you suggest, i get this error with your code: Exception in thread "main" java.lang.Error: Unresolved compilation problems: Unhandled exception type AWTException Unreachable catch block for AWTException. This exception is never thrown from the try statement body at com.z.RobotLib.(RobotLib.java:93) at com.z.Example.main(Example.java:9) – zeddex Aug 01 '12 at 03:54
1

Don't quite sure of your questions but I hope this might help!

You can throws an exception to avoid unnecessary try and catch blocks. Also, creating an instance of class Robot will make you avoid writing new Robot() at everyline you are in need of it.

Positive
  • 62
  • 5
0

I just found a way to do what i wanted using a modified version of the code Raskolnikov posted, it allows shorter versions of the function like i wanted:

public class RobotLib {

private static Robot robot;

public RobotLib(){
  try {
    robot = new Robot();
} catch (AWTException e) {
    e.printStackTrace();
}
}

// Press Function
public void Press(int key, int time){ 
    robot.keyPress(key);
    robot.delay(time);
    robot.keyRelease(key);
} 

}
zeddex
  • 1,260
  • 5
  • 21
  • 38