1

Is it possible to create multiple robots and have them run in multiple "desktops". I use a mac and it is possible to create multiple desktops (also known as spaces) and have many windows running in each. Is it possible to have multiple java command line tools using the robot class at once each running in a different desktop. If so, how would I go about doing this.

2
  • What have you tried so far? What was the problem that you encountered when you did that? Commented Jun 6, 2016 at 7:08
  • Since the Robot class has a constructor accepting a GraphicsDevice argument, the answer is "most likely". Try it and see what you get. Commented Jun 6, 2016 at 7:31

1 Answer 1

1

It is possible, in order to do that you'll need to use threads in java (also known as multi threading). Here is the sample program to help you understand. The threads won't run exactly at the same time, but will run simultaneously. To make them run simultaneously, the synchronization of the thread is required, which will make code more complex

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;

// Thread is what we are using

class Anything1 extends Thread {
boolean activate = true;

public void run() {
    // add here your robot class with actions
    while (activate) {
    try {
        Robot robot = new Robot();
    } catch (AWTException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Hello1");
}
}}

class Anything2 extends Thread {
boolean activate = true;

public void run() {
    // add here your robot class with actions
    while (activate) {
    try {
        Robot robot = new Robot();
    } catch (AWTException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // for testing
    System.out.println("Hello2");
}
}
}


public class App {
public static void main(String[] args) {
    // activates the process
    Anything1 p = new Anything1();
    Anything2 r = new Anything2();
    p.start();
    r.start();
}
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.