1

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!

Dream Lane
  • 1,282
  • 5
  • 16
  • 27
  • Please post whatever you are seeing... – Joseph Weissman Dec 07 '10 at 04:10
  • Are you using any sort of IDE? Eclipse? NetBeans? – Matt Ball Dec 07 '10 at 04:11
  • 2
    You should read up on object oriented programming. Classes don't perform tasks. Classes are instantiated to objects. And objects have methods that perform tasks. – Falmarri Dec 07 '10 at 04:30
  • @Falmarri, Thank you. What I want to do is not make a class, but a method of a class instead that performs a series of methods from the robot class. – Dream Lane Dec 07 '10 at 04:40
  • @Matt Ball, I am using the JME3 IDE, which I think is based on netbeans, but not sure – Dream Lane Dec 07 '10 at 04:45
  • Wow... I can't believe this question didn't get more downvotes... Thanks for being understanding everyone! Reading this question 6 months of CS studies later makes me cringe – Dream Lane Jul 01 '11 at 01:37

4 Answers4

2
public static void doStuff(Robot robot){
   // paste your stuff here
}

Make sure you've got the imports required to get through the compiler, and this should work

GBlodgett
  • 12,704
  • 4
  • 31
  • 45
ajwood
  • 18,227
  • 15
  • 61
  • 104
2

It's a good question. Two things to consider:

  1. use encapsulation to change the functionality of a class that doesn't quite do what you want. In other words, create a new class called RobotWrapper which has an instance of Robot.
  2. wrap a bothersome checked exception in a RuntimeException

Here's what I came up with:

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

public class RobotWrapper {

    private static final int STD_DELAY = 35000;

    private final Robot robot;

    public RobotWrapper() {
        try {
            robot = new Robot();
        } catch (AWTException e) {
            throw new RuntimeException(e);
        }
    }

    public void doStuff(int x, int y) {
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.mouseMove(x, y);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        robot.delay(STD_DELAY);
    }

    public static void main(String[] args) {
        RobotWrapper robotWrapper = new RobotWrapper();
        robotWrapper.doStuff(16, 853);
        robotWrapper.doStuff(100, 200);
    }
}
Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
  • This is nice. I'm wondering though, what makes this a nicer solution than my idea to sublass Robot? When is wrapping more appropriate than extending? – ajwood Dec 08 '10 at 18:55
  • Great answer thanks! I am unfamiliar with creating wrappers, but you answer has been helpful to me in understanding them. – Dream Lane Dec 08 '10 at 19:52
  • 1
    It's an OOP principle - prefer composition over inheritance...read arguments for and against here: http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance – Steve McLeod Dec 09 '10 at 13:28
-1

Note that some people think singletons are anti-patterns - this vastly depends on what you want to do, but will work for your simple case. You should read a bit more about this before you blindly use it.

You can use singleton pattern for this:

public class Singleton {

    private static final Singleton INSTANCE = new Singleton();

    // Private constructor prevents instantiation from other classes
    private Singleton() {
    }

    public static Singleton getInstance() {
        return INSTANCE;
    }

    public void doSomeStuff() {
        // Put your code here
    }

}

Then you can call it like this:

Singleton.getInstance().doSomeStuff()

Rename your class to something else that has some meaningful name.

Take a look here for an explanation:

icyrock.com
  • 27,952
  • 4
  • 66
  • 85
  • I don't think you necessarily want a singleton pattern. What is so wrong about having multiple robots? – ajwood Dec 07 '10 at 04:16
  • Agree, nothing wrong at all - there are certainly many different possible implementations, this is one of them. – icyrock.com Dec 07 '10 at 04:19
-1

You may think about subclassing Robot.

public class ExtendedRobot extends Robot{
public doStuff(){
// paste code here
}
}

To make your stuff happen, you'd make a robot.
Robot robot = new Robot();
robot.doStuff();

ajwood
  • 18,227
  • 15
  • 61
  • 104
  • I tried this, but I get an error: unreported exception java.awt.AWTException in default constructor. I'm not sure where to put the throws AWTException to make this work... – Dream Lane Dec 07 '10 at 04:47
  • Don't do this, this is absolutely not what you're asking for. – Falmarri Dec 07 '10 at 17:35
  • Could you explain why it's not what he's asking for? If this task is something that his robot should be able to perform, why is it unreasonable for it to be a method in a subclass? I see his questions as essentially looking for ways to avoid code duplication. – ajwood Dec 08 '10 at 00:06