Sorry if the question doesn't make sense. I've got a feeling that if I were able to better word the question I would be able to find the answer myself.
Here's what my code looks like:(omitting parts not pertaining to the question)
Robot robot = new Robot();// create robot
//---Perform Task ---//
//part 1
robot.keyPress(KeyEvent.VK_CONTROL);
robot.mouseMove(1300, var);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(stdDelay);
//part 2
robot.mouseMove(1300, var+20);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(stdDelay);
//part 3
robot.mouseMove(1300, var+40);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(stdDelay);
robot.keyRelease(KeyEvent.VK_CONTROL);
What I want to do is place all of that portion beneath //---Perform Task---// into a method so that I can just call that method when I want to have that task performed, rather than having to copy paste all of that code every time I want to perform that task.
To help clarify:
If I do this:
public class task {
Robot robot = new Robot();
//---Begin task-----//
robot.mouseMove(16,853);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
//---Wait for task---//
robot.delay(35000);
}
I get the error on Robot robot line: unreported exception java.awt.AWTException; must be caught or declared to be thrown. Which then leads to all the other lines erroring because they are dependent on the construction of the new robot.
Thanks a lot for all the suggestions already. I will give those a shot!
What I ended up doing was creating a class: public class RobotStuff{} with methods that looked like: Public void doStuff() throws AWTException{} Then I constructed a new Robot() in each method. It is working so far. Thanks for the comments and answers!