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