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(); } }
public RobotHandler() { try { robot = new Robot(); } 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(); } }
/** * Creates an instance of <tt>TransparentBackground</tt> by specifying the parent <tt>Window</tt> * - this is the window that should be made transparent. * * @param window The parent <tt>Window</tt> */ public TransparentBackground(Window window) { this.window = window; Robot rbt; try { rbt = new Robot(); } catch (AWTException e) { e.printStackTrace(); rbt = null; } this.robot = rbt; }
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); }
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 screenshot() { if (System.currentTimeMillis() - ultimaFoto > 1000) { try { Robot robot = new Robot(); Rectangle captureSize = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); BufferedImage bufferedImage = robot.createScreenCapture(captureSize); File outputfile = new File( System.getProperty("user.home") + "\\Desktop\\ScreenShot" + ultimaFoto + ".jpg"); ImageIO.write(bufferedImage, "jpg", outputfile); } catch (IOException e) { } catch (AWTException e) { // TODO Auto-generated catch block e.printStackTrace(); } ultimaFoto = System.currentTimeMillis(); } }
private void setTrayIcon(Stage primaryStage) throws ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException { if (!SystemTray.isSupported()) { return; } SystemTray sTray = SystemTray.getSystemTray(); primaryStage.setOnCloseRequest(arg0 -> primaryStage.hide()); JPopupMenu popup = buildSystemTrayJPopupMenu(primaryStage); URL url = System.class.getResource("/logo-invert_small.png"); Image img = Toolkit.getDefaultToolkit().getImage(url); TrayIcon icon = new TrayIcon(img, "Qabel"); icon.setImageAutoSize(true); trayIconListener(popup, icon); try { sTray.add(icon); } catch (AWTException e) { logger.error("failed to add tray icon: " + e.getMessage(), e); } }
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()
public static void main(String[] args) { System.out.println(ResourceUsage.getStatus()); final String ipAddress = Util.getIp(); final Thread listener; final SocketListener socketListener = new SocketListener(); listener = new Thread(socketListener); if (!SystemTray.isSupported()) { System.err.println("System tray is not supported."); return; } SystemTray systemTray = SystemTray.getSystemTray(); Image image = Toolkit.getDefaultToolkit().getImage(ServiceDriver.class.getResource("pause.png")); final TrayIcon trayIcon = new TrayIcon(image); final PopupMenu trayPopupMenu = new PopupMenu(); MenuItem startService = new MenuItem("Start Service"); startService.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog( null, "Service Started", "Surrogate Service", JOptionPane.INFORMATION_MESSAGE); try { listener.start(); Image image = Toolkit.getDefaultToolkit() .getImage(ServiceDriver.class.getResource("cyber.gif")); trayIcon.setImage(image); } catch (Exception err) { Image image = Toolkit.getDefaultToolkit() .getImage(ServiceDriver.class.getResource("cyber.gif")); trayIcon.setImage(image); socketListener.resume(); } } }); trayPopupMenu.add(startService); MenuItem action = new MenuItem("Stop Service"); action.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog( null, "Service Stopped", "Surrogate Service", JOptionPane.INFORMATION_MESSAGE); try { socketListener.pause(); Image image = Toolkit.getDefaultToolkit() .getImage(ServiceDriver.class.getResource("pause.png")); trayIcon.setImage(image); } catch (Exception e1) { System.err.println("Service has not stared yet"); } } }); trayPopupMenu.add(action); MenuItem close = new MenuItem("Close"); close.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); trayPopupMenu.add(close); trayIcon.setPopupMenu(trayPopupMenu); trayIcon.addMouseMotionListener( new MouseMotionAdapter() { @Override public void mouseMoved(MouseEvent e) { trayIcon.setToolTip(Util.getStatus(ipAddress)); } }); trayIcon.setImageAutoSize(true); try { systemTray.add(trayIcon); } catch (AWTException awtException) { awtException.printStackTrace(); } }
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."); } }
/** * @param args * @throws AWTException */ public static void main(String[] args) { Robot robot; // the size of a square on the board int cubeSize = 40; // the size of the sample to determine a gem's colour int sampleSize = 4; // the position of the top left of the board (in screen coordinates) int gameLeft = 507; int gameTop = 345; // game length (in milliseconds) long gameLength = 63 * 1000; // the maximum number of times (in a row) there can be no moves to make long noMoveCutoff = 5; // the maximum number of gems to move at the same time int maxMovesPerRound = 10; // save screenshots to disk boolean saveScreenshots = false; // the extent of the game board Rectangle gameRectangle = new Rectangle(gameLeft, gameTop, cubeSize * 8, cubeSize * 8); // the robot will move the mouse and perform clicks try { robot = new Robot(); } catch (AWTException e) { e.printStackTrace(); return; } final GameKeyListener frame = new GameKeyListener("Bejeweled AI Player"); JOptionPane.showConfirmDialog(frame, "Press yes to start..."); try { Thread.sleep(3500); } catch (Exception e) { } BufferedImage gameScreenshot; Board board = new Board(cubeSize, sampleSize); Move nextMove; // the gem colours are stored in a 2D array int[][] boardGems; // counts the number of times (in a row) no move has been available int noMoveCount = 0; // the number of screenshots saved to disk int screenshotCount = 0; long startTime = System.currentTimeMillis(); long endTime = startTime + gameLength; while (System.currentTimeMillis() < endTime) { if (noMoveCount > (noMoveCutoff - 1)) { System.out.println(noMoveCutoff + " no-move rounds in a row...exiting!"); System.exit(0); return; } // take a screenshot of the screen gameScreenshot = robot.createScreenCapture(gameRectangle); if (saveScreenshots) { File outputfile = new File("screenshot" + screenshotCount + ".jpg"); try { ImageIO.write(gameScreenshot, "jpg", outputfile); screenshotCount++; } catch (IOException e) { e.printStackTrace(); } } // figure out the positions of the gems on the board boardGems = board.getBoardGemColors(gameScreenshot); try { // calculate the best moves to make LinkedList<Move> moves = Solver.solve(boardGems); if (moves.size() > 0) { // Reset no-move counter noMoveCount = 0; int movesToMake = Math.min(moves.size(), maxMovesPerRound); for (int i = 0; i < movesToMake; i++) { nextMove = moves.get(i); robot.mouseMove( gameLeft + cubeSize / 2 + cubeSize * nextMove.x1, gameTop + cubeSize / 2 + cubeSize * nextMove.y1); robot.mousePress(InputEvent.BUTTON1_MASK); robot.mouseRelease(InputEvent.BUTTON1_MASK); robot.mouseMove( gameLeft + cubeSize / 2 + cubeSize * nextMove.x2, gameTop + cubeSize / 2 + cubeSize * nextMove.y2); robot.mousePress(InputEvent.BUTTON1_MASK); robot.mouseRelease(InputEvent.BUTTON1_MASK); } } } catch (NoMoveException e) { System.out.println("ERROR: No move."); noMoveCount++; try { Thread.sleep(600); } catch (Exception e2) { } } java.awt.EventQueue.invokeLater( new Runnable() { @Override public void run() { frame.toFront(); frame.repaint(); } }); try { // Pause a bit to let the game catch up! Thread.sleep(300); } catch (Exception e) { } } System.exit(0); }
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); }