Example #1
0
  private MainPanel(final JFrame frame) {
    super();
    add(check);
    setPreferredSize(new Dimension(320, 240));

    if (!SystemTray.isSupported()) {
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      return;
    }
    frame.addWindowListener(
        new WindowAdapter() {
          @Override
          public void windowIconified(WindowEvent e) {
            if (check.isSelected()) {
              e.getWindow().dispose();
            }
          }
        });
    // or
    // frame.addWindowStateListener(new WindowStateListener() {
    //    @Override public void windowStateChanged(WindowEvent e) {
    //        if (check.isSelected() && e.getNewState() == Frame.ICONIFIED) {
    //            e.getWindow().dispose();
    //        }
    //    }
    // });

    final SystemTray tray = SystemTray.getSystemTray();
    Dimension d = tray.getTrayIconSize();
    BufferedImage image = makeBufferedImage(new StarIcon(), d.width, d.height);
    final PopupMenu popup = new PopupMenu();
    final TrayIcon icon = new TrayIcon(image, "TRAY", popup);

    MenuItem item1 = new MenuItem("OPEN");
    item1.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            frame.setVisible(true);
          }
        });
    MenuItem item2 = new MenuItem("EXIT");
    item2.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            tray.remove(icon);
            frame.dispose();
            // frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
          }
        });
    popup.add(item1);
    popup.add(item2);

    try {
      tray.add(icon);
    } catch (AWTException e) {
      e.printStackTrace();
    }
  }
 // Set if the application is in editing mode based on the state of the shift key
 public void setEditingMode() {
   try {
     Robot robot = new Robot();
     robot.keyRelease(KeyEvent.VK_SHIFT);
   } catch (AWTException e) {
     e.printStackTrace();
   }
 }
  public static void main(String s[]) {

    // Getting save directory
    String saveDir;
    if (s.length > 0) {
      saveDir = s[0];
    } else {
      saveDir =
          JOptionPane.showInputDialog(
              null,
              "Please enter directory where "
                  + "the images is/will be saved\n\n"
                  + "Also possible to specifiy as argument 1 when "
                  + "running this program.",
              "l:\\webcamtest");
    }

    String layout = "";
    if (s.length > 1) {
      layout = s[1];
    }

    // Move mouse to the point 5000,5000 px (out of the screen)
    Robot rob;
    try {
      rob = new Robot();
      rob.setAutoDelay(500); // 0,5 s
      rob.mouseMove(5000, 5000);
    } catch (AWTException e) {
      e.printStackTrace();
    }

    // Make the main window
    JFrame frame = new JFrame();
    frame.setAlwaysOnTop(true);
    frame.setTitle(
        "Webcam capture and imagefading - "
            + "Vitenfabrikken Jærmuseet - "
            + "made by Hallvard Nygård - "
            + "Vitenfabrikken.no / Jaermuseet.no");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setUndecorated(true);

    WebcamCaptureAndFadePanel panel = new WebcamCaptureAndFadePanel(saveDir, layout);
    frame.getContentPane().add(panel);
    frame.addKeyListener(panel);
    frame.pack();

    frame.setVisible(true);
  }
Example #4
0
  public void install() {
    try {
      if (trayIcon == null && SystemTray.isSupported()) {
        SystemTray systemTray = SystemTray.getSystemTray();
        Dimension size = systemTray.getTrayIconSize();
        trayIcon = createTrayIcon(size);
        systemTray.add(trayIcon);

        JPopupMenu popup = new JPopupMenu();
        trayIcon.setJPopupMenu(popup);
        createPopup(popup);
      }
    } catch (AWTException e) {
      e.printStackTrace();
    }
  }
  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()
Example #6
0
  private void test(TestState currentState) throws InterruptedException, InvocationTargetException {

    synchronized (LOCK) {
      this.currentState = currentState;
      System.out.println(this.currentState);

      List list;
      if (currentState.getMultiple()) {
        list = multiple;
      } else {
        list = single;
      }

      Robot r;
      try {
        r = new Robot();
      } catch (AWTException e) {
        throw new RuntimeException(e.getMessage());
      }

      r.delay(10);
      Point loc = this.getLocationOnScreen();

      r.mouseMove(loc.x + 10, loc.y + 10);
      r.mousePress(InputEvent.BUTTON1_MASK);
      r.delay(10);
      r.mouseRelease(InputEvent.BUTTON1_MASK);
      r.delay(10);

      list.requestFocusInWindow();
      LOCK.wait(ACTION_TIMEOUT);
      if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != list) {
        throw new RuntimeException("Test failed - list isn't focus owner.");
      }

      list.deselect(0);
      list.deselect(1);
      list.deselect(2);
      list.deselect(3);
      list.deselect(4);
      list.deselect(5);
      list.deselect(6);
      list.deselect(7);
      list.deselect(8);

      int selectIndex = 0;
      int visibleIndex = 0;

      if (currentState.getScrollMoved()) {

        if (currentState.getKeyID() == KeyEvent.VK_PAGE_UP
            || currentState.getKeyID() == KeyEvent.VK_HOME) {
          selectIndex = 8;
          visibleIndex = 8;
        } else if (currentState.getKeyID() == KeyEvent.VK_PAGE_DOWN
            || currentState.getKeyID() == KeyEvent.VK_END) {
          selectIndex = 0;
          visibleIndex = 0;
        }

      } else {

        if (currentState.getKeyID() == KeyEvent.VK_PAGE_UP
            || currentState.getKeyID() == KeyEvent.VK_HOME) {

          if (currentState.getSelectedMoved()) {
            selectIndex = 1;
            visibleIndex = 0;
          } else {
            selectIndex = 0;
            visibleIndex = 0;
          }

        } else if (currentState.getKeyID() == KeyEvent.VK_PAGE_DOWN
            || currentState.getKeyID() == KeyEvent.VK_END) {

          if (currentState.getSelectedMoved()) {
            selectIndex = 7;
            visibleIndex = 8;
          } else {
            selectIndex = 8;
            visibleIndex = 8;
          }
        }
      }

      list.select(selectIndex);
      list.makeVisible(visibleIndex);

      r.delay(10);

      if (currentState.getKeyID() == KeyEvent.VK_HOME
          || currentState.getKeyID() == KeyEvent.VK_END) {
        r.keyPress(KeyEvent.VK_CONTROL);
      }

      r.delay(10);
      r.keyPress(currentState.getKeyID());
      r.delay(10);
      r.keyRelease(currentState.getKeyID());
      r.delay(10);

      if (currentState.getKeyID() == KeyEvent.VK_HOME
          || currentState.getKeyID() == KeyEvent.VK_END) {
        r.keyRelease(KeyEvent.VK_CONTROL);
      }

      r.waitForIdle();
      r.delay(200);

      if (currentState.getTemplate() != currentState.getAction())
        throw new RuntimeException("Test failed.");
    }
  }
  private void installPipette() {
    if (pipettePicker == null) {
      // Pipette picker icon
      pipettePicker = new WebImage(pipetteIcon);
      //            pipettePicker.setMargin ( 0, 2, 0, 2 );

      // Pipette picker actions
      try {
        robot = new Robot();
      } catch (final AWTException e) {
        e.printStackTrace();
      }
      if (robot != null) {
        final MouseAdapter mouseAdapter =
            new MouseAdapter() {
              private boolean shouldUpdateColor;

              private WebDialog window;
              private WebPanel screen;
              private WebLabel info;

              private boolean updating = false;
              private BufferedImage screenshot;
              private Color color;

              @Override
              public void mousePressed(final MouseEvent e) {
                if (pipetteEnabled && SwingUtils.isLeftMouseButton(e)) {
                  // Resetting color update mark
                  shouldUpdateColor = true;

                  // Creating preview window
                  createPreviewWindow();
                  updateWindowLocation();

                  // Displaying preview window
                  window.pack();
                  window.setVisible(true);

                  // Transferring focus to preview panel
                  screen.requestFocus();

                  // Updating preview screenshot
                  updateScreenshot();
                }
              }

              @Override
              public void mouseDragged(final MouseEvent e) {
                if (pipetteEnabled && SwingUtils.isLeftMouseButton(e) && window != null) {
                  // Updating preview window location
                  updateWindowLocation();

                  // Updating preview screenshot
                  updateScreenshot();
                }
              }

              @Override
              public void mouseReleased(final MouseEvent e) {
                if (pipetteEnabled && SwingUtils.isLeftMouseButton(e) && window != null) {
                  // Closing preview window
                  window.dispose();
                }
              }

              private void updateScreenshot() {
                // Simply ignore update if an old one is still running
                if (!updating) {
                  // Updating image in a separate thread to avoid UI freezing
                  updating = true;
                  new Thread(
                          new Runnable() {
                            @Override
                            public void run() {
                              if (screen != null) {
                                final Point p = MouseInfo.getPointerInfo().getLocation();
                                screenshot =
                                    robot.createScreenCapture(
                                        new Rectangle(
                                            p.x - pipettePixels / 2,
                                            p.y - pipettePixels / 2,
                                            pipettePixels,
                                            pipettePixels));
                                color =
                                    new Color(
                                        screenshot.getRGB(pipettePixels / 2, pipettePixels / 2));
                                if (screen != null) {
                                  screen.repaint();
                                  info.setText(getColorText(color));
                                } else {
                                  screenshot.flush();
                                  screenshot = null;
                                  color = null;
                                }
                              }
                              updating = false;
                            }
                          })
                      .start();
                }
              }

              private void createPreviewWindow() {
                window = new WebDialog(pipettePicker);
                window.setLayout(new BorderLayout());
                window.setUndecorated(true);
                window.setAlwaysOnTop(true);

                window.addWindowListener(
                    new WindowAdapter() {
                      @Override
                      public void windowClosed(final WindowEvent e) {
                        if (screenshot != null) {
                          if (shouldUpdateColor) {
                            setColor(color);
                          }
                          screenshot.flush();
                          screenshot = null;
                        }
                        HotkeyManager.unregisterHotkeys(screen);
                        window = null;
                        screen = null;
                      }
                    });

                final AbstractPainter<WebPanel> screenPainter =
                    new AbstractPainter<WebPanel>() {
                      /** {@inheritDoc} */
                      @Override
                      public void paint(
                          final Graphics2D g2d, final Rectangle bounds, final WebPanel c) {
                        if (window.isShowing() && robot != null) {
                          // Screen
                          g2d.drawImage(
                              screenshot,
                              bounds.x + 2,
                              bounds.y + 2,
                              bounds.width - 4,
                              bounds.height - 4,
                              null);

                          // Border
                          g2d.setPaint(Color.BLACK);
                          g2d.drawRect(0, 0, bounds.width - 1, bounds.height - 1);
                          g2d.setPaint(Color.WHITE);
                          g2d.drawRect(1, 1, bounds.width - 3, bounds.height - 3);

                          // Cursor
                          final int mx = bounds.x + bounds.width / 2;
                          final int my = bounds.y + bounds.height / 2;
                          g2d.setPaint(Color.WHITE);
                          g2d.drawLine(mx - 1, my - 7, mx - 1, my + 7);
                          g2d.drawLine(mx + 1, my - 7, mx + 1, my + 7);
                          g2d.drawLine(mx - 7, my - 1, mx + 7, my - 1);
                          g2d.drawLine(mx - 7, my + 1, mx + 7, my + 1);
                          g2d.setPaint(Color.BLACK);
                          g2d.drawLine(mx, my - 7, mx, my + 7);
                          g2d.drawLine(mx - 7, my, mx + 7, my);
                        }
                      }
                    };

                screen = new WebPanel(screenPainter);
                screen.setFocusable(true);
                screen.setPreferredSize(
                    new Dimension(pipettePixels * pixelSize + 4, pipettePixels * pixelSize + 4));
                window.add(screen, BorderLayout.CENTER);

                info = new WebLabel(WebLabel.LEADING);
                info.setMargin(4);
                info.setIcon(
                    new Icon() {
                      @Override
                      public void paintIcon(
                          final Component c, final Graphics g, final int x, final int y) {
                        if (color != null) {
                          final Graphics2D g2d = (Graphics2D) g;
                          g2d.setPaint(Color.BLACK);
                          g2d.drawRect(x, y, 15, 15);
                          g2d.setPaint(Color.WHITE);
                          g2d.drawRect(x + 1, y + 1, 13, 13);
                          g2d.setPaint(color);
                          g2d.fillRect(x + 2, y + 2, 12, 12);
                        }
                      }

                      @Override
                      public int getIconWidth() {
                        return 16;
                      }

                      @Override
                      public int getIconHeight() {
                        return 16;
                      }
                    });
                info.setPainter(
                    new AbstractPainter<WebLabel>() {
                      /** {@inheritDoc} */
                      @Override
                      public Insets getMargin(final WebLabel c) {
                        return new Insets(0, 2, 2, 2);
                      }

                      /** {@inheritDoc} */
                      @Override
                      public void paint(
                          final Graphics2D g2d, final Rectangle bounds, final WebLabel c) {
                        g2d.setPaint(Color.BLACK);
                        g2d.drawRect(bounds.x, bounds.y - 1, bounds.width - 1, bounds.height);
                      }
                    });
                window.add(info, BorderLayout.SOUTH);

                HotkeyManager.registerHotkey(
                    screen,
                    Hotkey.ESCAPE,
                    new HotkeyRunnable() {
                      @Override
                      public void run(final KeyEvent e) {
                        if (window != null) {
                          shouldUpdateColor = false;
                          window.dispose();
                        }
                      }
                    });
              }

              private void updateWindowLocation() {
                final Point p = MouseInfo.getPointerInfo().getLocation();
                final Rectangle b =
                    window
                        .getGraphicsConfiguration()
                        .getDevice()
                        .getDefaultConfiguration()
                        .getBounds();
                final int ww = window.getWidth();
                final int wh = window.getHeight();
                final int x = p.x + 20 + ww < b.x + b.width ? p.x + 20 : p.x - 20 - ww;
                final int y = p.y + 20 + wh < b.y + b.height ? p.y + 20 : p.y - 20 - wh;
                window.setLocation(x, y);
              }
            };
        pipettePicker.addMouseListener(mouseAdapter);
        pipettePicker.addMouseMotionListener(mouseAdapter);
        pipettePicker.setCursor(Cursor.getDefaultCursor());
      }
    }

    // Adding field leading component
    setLeadingComponent(pipettePicker);
  }