Search in sources :

Example 16 with AWTException

use of java.awt.AWTException in project HearthStats.net-Uploader by HearthStats.

the class JnaUtil method main.

public static void main(String[] args) throws InterruptedException {
    String[] testStrs = { "Untitled-Notepad", "Untitled - Notepad", "Untitled  -  Notepad", "Java-Epic", "Java - Epic", "Fubars rule!", "The First Night", "New Tab", "Citrix X", "EHR PROD - SVC" };
    for (String testStr : testStrs) {
        Pointer hWnd = getWinHwnd(testStr);
        boolean isWindow = windowExists(hWnd);
        System.out.printf("%-22s %5b %16s %b%n", testStr, windowExists(testStr), hWnd, isWindow);
    }
    String ehrProd = "EHR PROD - SVC";
    Pointer hWnd = getWinHwnd(ehrProd);
    System.out.println("is it foreground window? " + isForegroundWindow(hWnd));
    boolean foo = setForegroundWindow(ehrProd);
    System.out.println("foregroundwindow: " + foo);
    Thread.sleep(400);
    System.out.println("is it foreground window? " + isForegroundWindow(hWnd));
    Thread.sleep(400);
    try {
        Rectangle rect = getWindowRect(ehrProd);
        Robot robot = new Robot();
        BufferedImage img = robot.createScreenCapture(rect);
        ImageIcon icon = new ImageIcon(img);
        JLabel label = new JLabel(icon);
        JOptionPane.showMessageDialog(null, label);
    } catch (AWTException e) {
        e.printStackTrace();
    } catch (JnaUtilException e) {
        e.printStackTrace();
    }
}
Also used : ImageIcon(javax.swing.ImageIcon) Rectangle(java.awt.Rectangle) JLabel(javax.swing.JLabel) Pointer(com.sun.jna.Pointer) Robot(java.awt.Robot) BufferedImage(java.awt.image.BufferedImage) AWTException(java.awt.AWTException)

Example 17 with AWTException

use of java.awt.AWTException in project otapij by FellowTraveler.

the class MainPage method setToSystray.

private void setToSystray() {
    Image image = null;
    System.out.println("creating instance");
    if (SystemTray.isSupported()) {
        System.out.println("system tray supported");
        tray = SystemTray.getSystemTray();
        ImageIcon image1 = new javax.swing.ImageIcon(getClass().getResource("/com/moneychanger/ui/images/images.jpeg"));
        image = image1.getImage();
        ActionListener exitListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Exiting....");
                System.exit(0);
            }
        };
        PopupMenu popup = new PopupMenu();
        MenuItem defaultItem = new MenuItem("Exit");
        defaultItem.addActionListener(exitListener);
        popup.add(defaultItem);
        defaultItem = new MenuItem("Open");
        defaultItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //tray.remove(trayIcon);
                setVisible(true);
                repaint();
                setVisible(true);
                System.out.println("Open");
            }
        });
        popup.add(defaultItem);
        trayIcon = new TrayIcon(image, "Moneychanger", popup);
        trayIcon.setImageAutoSize(true);
    } else {
        System.out.println("system tray not supported");
    }
    final String os = System.getProperty("os.name").toLowerCase();
    addWindowStateListener(new WindowStateListener() {

        @Override
        public void windowStateChanged(WindowEvent e) {
            System.out.println("-----:" + e.getNewState());
            if (e.getNewState() == ICONIFIED) {
                try {
                    tray.add(trayIcon);
                    if (os.indexOf("nix") < 0 || os.indexOf("nux") < 0) {
                        setVisible(false);
                    }
                    System.out.println("added to SystemTray");
                } catch (AWTException ex) {
                    System.out.println("unable to add to tray");
                }
            }
            if (e.getNewState() == 7) {
                try {
                    tray.add(trayIcon);
                    if (!(os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0)) {
                        setVisible(false);
                    }
                    System.out.println("added to SystemTray");
                } catch (AWTException ex) {
                    System.out.println("unable to add to system tray");
                }
            }
            if (e.getNewState() == MAXIMIZED_BOTH) {
                tray.remove(trayIcon);
                setVisible(true);
                System.out.println("Max both");
                System.out.println("Tray icon removed");
            }
            if (e.getNewState() == NORMAL) {
                tray.remove(trayIcon);
                setVisible(true);
                System.out.println("Max NORMAL");
                System.out.println("Tray icon removed");
            }
        }
    });
    //  setIconImage(Toolkit.getDefaultToolkit().getImage("Duke256.png"));
    if (image != null) {
        setIconImage(image);
    }
}
Also used : ImageIcon(javax.swing.ImageIcon) WindowStateListener(java.awt.event.WindowStateListener) ActionEvent(java.awt.event.ActionEvent) MenuItem(java.awt.MenuItem) JMenuItem(javax.swing.JMenuItem) Image(java.awt.Image) ActionListener(java.awt.event.ActionListener) TrayIcon(java.awt.TrayIcon) WindowEvent(java.awt.event.WindowEvent) PopupMenu(java.awt.PopupMenu) JPopupMenu(javax.swing.JPopupMenu) AWTException(java.awt.AWTException)

Example 18 with AWTException

use of java.awt.AWTException in project jdk8u_jdk by JetBrains.

the class WindowDragTest method main.

public static void main(String[] args) {
    try {
        Robot robot = new Robot();
        robot.setAutoDelay(1000);
        Frame frame1 = new Frame();
        frame1.setBounds(50, 50, 300, 200);
        frame1.setVisible(true);
        frame1.toFront();
        frame1.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // Clicking frame1 succeeded - mouse is not captured
                passed = true;
            }
        });
        robot.delay(1000);
        Frame frame2 = new Frame();
        frame2.setBounds(100, 100, 300, 200);
        frame2.setVisible(true);
        frame2.toFront();
        robot.delay(1000);
        Point p = frame2.getLocationOnScreen();
        Dimension d = frame2.getSize();
        // Move cursor to frame2 title bar to drag
        robot.mouseMove(p.x + (int) (d.getWidth() / 2), p.y + (int) frame2.getInsets().top / 2);
        Util.waitForIdle(robot);
        // Start window dragging
        robot.mousePress(InputEvent.BUTTON1_MASK);
        Util.waitForIdle(robot);
        // Dispose window being dragged
        frame2.dispose();
        Util.waitForIdle(robot);
        // Release mouse button to be able to get MOUSE_CLICKED event on Util.clickOnComp()
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        Util.waitForIdle(robot);
        // Click frame1 to check whether mouse is not captured by frame2
        Util.clickOnComp(frame1, robot);
        Util.waitForIdle(robot);
        frame1.dispose();
        if (passed) {
            System.out.println("Test passed.");
        } else {
            System.out.println("Test failed.");
            throw new RuntimeException("Test failed.");
        }
    } catch (AWTException e) {
        throw new RuntimeException("AWTException occurred - problem creating robot!");
    }
}
Also used : Frame(java.awt.Frame) MouseEvent(java.awt.event.MouseEvent) MouseAdapter(java.awt.event.MouseAdapter) Point(java.awt.Point) Dimension(java.awt.Dimension) Robot(java.awt.Robot) AWTException(java.awt.AWTException)

Example 19 with AWTException

use of java.awt.AWTException in project jdk8u_jdk by JetBrains.

the class ExtraMouseClick method start.

//End  init()
public void start() {
    frame.setSize(480, 300);
    frame.setVisible(true);
    try {
        robot = new Robot();
    } catch (AWTException e) {
        throw new RuntimeException(e);
    }
    //a time to show Frame
    Util.waitForIdle(robot);
    fp = frame.getLocationOnScreen();
    for (int i = 0; i < TRIALS; i++) {
        checkClicked();
        clearFlags();
    }
    for (int i = 0; i < TRIALS; i++) {
        oneDrag(2);
        clearFlags();
    }
    for (int i = 0; i < TRIALS; i++) {
        oneDrag(5);
        clearFlags();
    }
    for (int i = 0; i < TRIALS; i++) {
        oneDrag(70);
        clearFlags();
    }
    //Check that no Drag event occur in the SMUDGE area
    String sToolkit = Toolkit.getDefaultToolkit().getClass().getName();
    System.out.println("Toolkit == " + sToolkit);
    if ("sun.awt.windows.WToolkit".equals(sToolkit)) {
        int dragWidth = ((Integer) Toolkit.getDefaultToolkit().getDesktopProperty("win.drag.width")).intValue();
        int dragHeight = ((Integer) Toolkit.getDefaultToolkit().getDesktopProperty("win.drag.height")).intValue();
        System.out.println("dragWidth==" + dragWidth + ":: dragHeight==" + dragHeight);
        // DragWidth and dragHeight may be equal to 1. In that case the SMUDGE rectangle
        // narrowed into 1x1 pixel and we can't drag a mouse in it.
        // In that case we may skip following testcase but I'd prefer if we move mouse on 1 pixel only.
        // And that should pass as well.
        dragWidth = dragWidth > 1 ? dragWidth / 2 : 1;
        dragHeight = dragHeight > 1 ? dragHeight / 2 : 1;
        for (int i = 0; i < TRIALS; i++) {
            smallWin32Drag(dragWidth, dragHeight);
            clearFlags();
        }
    } else if ("sun.lwawt.macosx.LWCToolkit".equals(sToolkit)) {
    // On MacOS X every mouse move event is MOUSE_DRAGGED event and
    // MOUSE_DRAGGED is sent back form the Native code to the java code
    // for every mouse move. Therefore 'smallDrag test should be
    // disabled for toolkit 'sun.lwawt.macosx.LWCToolkit'.
    } else {
        for (int i = 0; i < TRIALS; i++) {
            //on Motif and XAWT SMUDGE area is 4-pixels wide
            smallDrag(SMUDGE_WIDTH - 1, SMUDGE_HEIGHT - 1);
            clearFlags();
        }
    }
    System.out.println("Test passed.");
}
Also used : Robot(java.awt.Robot) Point(java.awt.Point) AWTException(java.awt.AWTException)

Example 20 with AWTException

use of java.awt.AWTException in project jdk8u_jdk by JetBrains.

the class DisposeFrameOnDragTest method main.

public static void main(String[] args) throws Throwable {
    SwingUtilities.invokeAndWait(new Runnable() {

        @Override
        public void run() {
            constructTestUI();
        }
    });
    Util.waitForIdle(null);
    try {
        Point loc = textArea.getLocationOnScreen();
        Util.drag(new Robot(), new Point((int) loc.x + 3, (int) loc.y + 3), new Point((int) loc.x + 40, (int) loc.y + 40), InputEvent.BUTTON1_MASK);
    } catch (AWTException ex) {
        throw new RuntimeException("Could not initiate a drag operation");
    }
    Util.waitForIdle(null);
}
Also used : Point(java.awt.Point) Robot(java.awt.Robot) AWTException(java.awt.AWTException)

Aggregations

AWTException (java.awt.AWTException)22 Robot (java.awt.Robot)13 BufferedImage (java.awt.image.BufferedImage)7 Point (java.awt.Point)5 Rectangle (java.awt.Rectangle)5 Frame (java.awt.Frame)4 IOException (java.io.IOException)4 Image (java.awt.Image)3 MenuItem (java.awt.MenuItem)3 PopupMenu (java.awt.PopupMenu)3 TrayIcon (java.awt.TrayIcon)3 ActionEvent (java.awt.event.ActionEvent)3 WindowEvent (java.awt.event.WindowEvent)3 File (java.io.File)3 BufferCapabilities (java.awt.BufferCapabilities)2 Dimension (java.awt.Dimension)2 Graphics (java.awt.Graphics)2 GraphicsConfiguration (java.awt.GraphicsConfiguration)2 ImageCapabilities (java.awt.ImageCapabilities)2 Insets (java.awt.Insets)2