0

I wanted to make a Java application so record and replay what the mouse is doing. Right now I have this code:

static JFrame j = new JFrame();
static JPanel p = new JPanel();
static JButton record = new JButton("RECORD");
static JButton replay = new JButton("REPLAY");
static JButton stop = new JButton("STOP");

public static boolean btnrec;
public static boolean btnrepl;
public static boolean btnstp = false;

public static boolean enRec = true;
public static boolean enRepl = false;
public static boolean enStp = false;

public static boolean clicked;

public static boolean rest = false;



static ArrayList<Point> pos = new ArrayList<>();
static ArrayList<Integer> time = new ArrayList<>();
static ArrayList<Boolean> leftClicked = new ArrayList<>();
static ArrayList<Boolean> rightClicked = new ArrayList<>();

static int timer;

static int keep;

static boolean mouseClicked;

static boolean rec = true;
static boolean repl;
static boolean stp;

public static void main(String[] args) {

    j.setTitle("Repeate Mouse");
    j.setAlwaysOnTop(true);
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    j.setSize(350, 200);
    j.setResizable(false);
    j.setLocation(new Point(420, 420));
    j.setVisible(true);

    stop.setEnabled(enStp);

    record.setSize(175, 40);
    record.setEnabled(enRec);

    replay.setSize(175, 40);
    replay.setEnabled(enRepl);

    record.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            btnrec = true;
            btnrepl = false;
            replay.setEnabled(true);
            stop.setEnabled(false);
            record.setEnabled(false);
            j.setTitle("Recording...");
        }
    });

    replay.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            btnrepl = true;
            btnrec = false;
            replay.setEnabled(false);
            stop.setEnabled(true);
            record.setEnabled(false);
        }
    });
    stop.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            btnstp = true;
            btnrepl = false;
            replay.setEnabled(true);
            stop.setEnabled(false);
            record.setEnabled(true);
        }
    });

    p.add(record);
    p.add(replay);
    p.add(stop);
    j.add(p);
    j.addMouseListener(new MouseListener() {

        @Override
        public void mouseReleased(MouseEvent e) {
            clicked = false;

        }

        @Override
        public void mousePressed(MouseEvent e) {
            clicked = true;

        }

        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseClicked(MouseEvent e) {
            // TODO Auto-generated method stub

        }
    });

    timer = -1;
    try {
        Thread.sleep(100);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    while(j.isVisible()) {
        System.out.println("");
        if(btnstp) {
            stop();
        }
        if(btnrec) {
            record();
        }
        if(btnrepl) {
            replay();
        }

    }

}

protected static void record() {
    pos.add(new Point(MouseInfo.getPointerInfo().getLocation()));
    time.add(timer);
    leftClicked.add(clicked);
    timer++;
    System.out.println(pos.get(timer));
    System.out.print(leftClicked.get(timer));
    j.setTitle("Recording....");
}

protected static void replay() {
    if(!rest) {
        timer = 0;
        rest = true;
    }
    if(leftClicked.get(timer)) {
        try {
            MouseAPI.genLeft();
        } catch (Exception e) {}
    }
    try {
        MouseAPI.move(pos.get(timer));
    } catch (Exception e) {}
    if(timer != time.size()-1) {
        timer++;
    } 
    else {
        rest = false;
    }

}

protected static void stop() {

}

So as you can see I'm storing all of the Points and clicks of the mouse in an ArrayList. My problem is, that this program only records clicks, that happend in the JFrame. Is there a way to listen for clicks that happen outside of the JFrame?

Thanks a lot!

Thirler
  • 20,239
  • 14
  • 63
  • 92
akaruikage
  • 141
  • 1
  • 8
  • This is a poorly designed class. You should not be using static variables and methods. – camickr Jan 03 '17 at 15:24
  • Well. I made the methods and variables static, because else i cant use them in the main method, which is static. – akaruikage Jan 04 '17 at 07:17
  • And that is why I said you have a poorly designed class. You should not be having all your logic in the main() method. For example check out the `ButtonDemo` found in the Swing tutorial found in [How to Use Buttons](http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/ButtonDemoProject/src/components/ButtonDemo.java) for a better way to structure your code. Keep a link to the tutorial handy for all Swing basics. Learn from working examples. – camickr Jan 04 '17 at 15:31

0 Answers0