예제 #1
0
  public void init() {
    try {
      robot = new Robot();
    } catch (AWTException ex) {
      ex.printStackTrace();
      throw new RuntimeException(ex);
    }
    this.setLayout(new BorderLayout());

    target.setBackground(Color.green);
    target.setName("GreenBox"); // for the ease of debug
    target.setPreferredSize(new Dimension(100, 100));
    String toolkit = Toolkit.getDefaultToolkit().getClass().getName();

    // on X systems two buttons are reserved for wheel though they are countable by MouseInfo.
    int buttonsNumber =
        toolkit.equals("sun.awt.windows.WToolkit")
            ? MouseInfo.getNumberOfButtons()
            : MouseInfo.getNumberOfButtons() - 2;

    for (int i = 0; i < 8; i++) {
      buttonNumber.add("BUTTON" + (i + 1) + "_MASK");
    }

    pressOn.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            System.out.println("Now pressing : " + (buttonNumber.getSelectedIndex() + 1));

            Timer timer = new Timer();
            TimerTask robotInteraction =
                new TimerTask() {
                  public void run() {
                    robot.mouseMove(updateTargetLocation().x, updateTargetLocation().y);
                    robot.mousePress(getMask(buttonNumber.getSelectedIndex() + 1));
                  }
                };
            timer.schedule(robotInteraction, SEND_DELAY);
          }
        });

    releaseOn.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            System.out.println("Now releasing : " + (buttonNumber.getSelectedIndex() + 1));
            Timer timer = new Timer();
            TimerTask robotInteraction =
                new TimerTask() {
                  public void run() {
                    robot.mouseMove(updateTargetLocation().x, updateTargetLocation().y);
                    robot.mouseRelease(getMask(buttonNumber.getSelectedIndex() + 1));
                  }
                };
            timer.schedule(robotInteraction, SEND_DELAY);
          }
        });

    clickOn.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            System.out.println("Now clicking : " + (buttonNumber.getSelectedIndex() + 1));
            Timer timer = new Timer();
            TimerTask robotInteraction =
                new TimerTask() {
                  public void run() {
                    robot.mouseMove(updateTargetLocation().x, updateTargetLocation().y);
                    robot.mousePress(getMask(buttonNumber.getSelectedIndex() + 1));
                    robot.mouseRelease(getMask(buttonNumber.getSelectedIndex() + 1));
                  }
                };
            timer.schedule(robotInteraction, SEND_DELAY);
          }
        });
    target.addMouseListener(
        new MouseAdapter() {
          public void mousePressed(MouseEvent e) {
            Sysout.println("" + e);
          }

          public void mouseReleased(MouseEvent e) {
            Sysout.println("" + e);
          }

          public void mouseClicked(MouseEvent e) {
            Sysout.println("" + e);
          }
        });

    String[] instructions = {
      "Do provide an instruction to the robot by",
      "choosing the button number to act and ",
      "pressing appropriate java.awt.Button on the left.",
      "Inspect an output in the TextArea below.",
      "Please don't generate non-natural sequences like Release-Release, etc.",
      "If you use keyboard be sure that you released the keyboard shortly.",
      "If events are generated well press Pass, otherwise Fail."
    };
    Sysout.createDialogWithInstructions(instructions);
  } // End  init()
예제 #2
0
 private Panel getPanel() {
   if (panel != null) return panel;
   panel = new Panel(viewSize);
   panel.setPreferredSize(viewSize.getDimension());
   return panel;
 }