Mathew
Mathew

Reputation: 1460

java robot class, multiple robots

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.

Upvotes: 1

Views: 459

Answers (1)

Albert Lipaev
Albert Lipaev

Reputation: 76

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();
}
}

Upvotes: 1

Related Questions