Search in sources :

Example 1 with AWTException

use of java.awt.AWTException in project screenbird by adamhub.

the class VideoCacheTest method generateTestImage.

/**
     * Generates screenshot for JUnit testing
     * @return 
     *      File pointer to screen shot
     */
private File generateTestImage() {
    Robot awtRobot;
    String currentCaptureDir = Settings.SCREEN_CAPTURE_DIR;
    try {
        awtRobot = new Robot();
        BufferedImage bufferedImage = awtRobot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter writer = (ImageWriter) iter.next();
        ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(1.0F);
        File imageFile = new File(currentCaptureDir, "test.jpeg");
        FileImageOutputStream stream = new FileImageOutputStream(imageFile);
        //Set up file access
        writer.setOutput(stream);
        //Create image
        IIOImage image = new IIOImage(bufferedImage, null, null);
        //write image
        writer.write(null, image, iwp);
        //Close image stream
        stream.close();
        return imageFile;
    } catch (AWTException e) {
        System.err.println(e);
    } catch (IOException e) {
        System.err.println(e);
    }
    return null;
}
Also used : FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) Rectangle(java.awt.Rectangle) Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) Robot(java.awt.Robot) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage) AWTException(java.awt.AWTException)

Example 2 with AWTException

use of java.awt.AWTException in project jna by java-native-access.

the class User32Test method testRegisterHotKey.

@Test
public void testRegisterHotKey() {
    int vk = KeyEvent.VK_D;
    int id = 1;
    assertTrue("RegisterHotKey failed", INSTANCE.RegisterHotKey(null, id, WinUser.MOD_CONTROL | WinUser.MOD_ALT, vk));
    Robot robot = null;
    try {
        robot = new Robot();
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_ALT);
        robot.keyPress(vk);
        Thread.sleep(50);
        robot.keyRelease(vk);
        WinUser.MSG msg = waitForMessage(500);
        assertNotNull(msg);
        assertEquals(msg.wParam.intValue(), id);
        assertTrue(INSTANCE.UnregisterHotKey(null, id));
        robot.keyPress(vk);
        Thread.sleep(10);
        robot.keyRelease(vk);
        msg = waitForMessage(500);
        assertNull(msg);
    } catch (AWTException e) {
        e.printStackTrace();
        fail();
    } catch (InterruptedException e) {
        e.printStackTrace();
        fail();
    } finally {
        if (robot != null) {
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyRelease(KeyEvent.VK_ALT);
        }
    }
}
Also used : Robot(java.awt.Robot) AWTException(java.awt.AWTException) Test(org.junit.Test)

Example 3 with AWTException

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

the class bug8016356 method main.

public static void main(String[] args) throws Exception {
    // Windows only test
    if (OSInfo.getOSType() == OSInfo.OSType.WINDOWS) {
        // Retrieving top edge of Desktop
        GraphicsConfiguration grConf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
        Rectangle scrRect = grConf.getBounds();
        Insets scrInsets = Toolkit.getDefaultToolkit().getScreenInsets(grConf);
        scrTop = scrRect.y + scrInsets.top;
        color = new Color(0, 255, 0);
        SwingUtilities.invokeAndWait(() -> {
            createAndShowUI();
        });
        try {
            Robot robot = new Robot();
            robot.setAutoDelay(500);
            robot.setAutoWaitForIdle(true);
            robot.delay(1000);
            // Resizing a window to invoke Windows Snap feature
            readFrameInfo();
            robot.mouseMove(frLoc.x + frSize.width / 2, frLoc.y);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseMove(frLoc.x + frSize.width / 2, scrTop);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            // Retrieving the color of window expanded area
            readFrameInfo();
            Insets insets = frame.getInsets();
            Color bgColor = robot.getPixelColor(frLoc.x + frSize.width / 2, frLoc.y + frSize.height - insets.bottom - 1);
            frame.dispose();
            if (!bgColor.equals(color)) {
                throw new RuntimeException("TEST FAILED: got " + bgColor + " instead of " + color);
            }
            System.out.println("TEST PASSED!");
        } catch (AWTException ex) {
            throw new RuntimeException("TEST FAILED!");
        }
    }
}
Also used : Insets(java.awt.Insets) Color(java.awt.Color) Rectangle(java.awt.Rectangle) Robot(java.awt.Robot) GraphicsConfiguration(java.awt.GraphicsConfiguration) AWTException(java.awt.AWTException)

Example 4 with AWTException

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

the class FullScreenInsets method testColor.

private static void testColor(final Window w, final Color color) {
    final Robot r;
    try {
        r = new Robot(w.getGraphicsConfiguration().getDevice());
    } catch (AWTException e) {
        e.printStackTrace();
        passed = false;
        return;
    }
    final BufferedImage bi = r.createScreenCapture(w.getBounds());
    for (int y = 0; y < bi.getHeight(); y++) {
        for (int x = 0; x < bi.getWidth(); x++) {
            if (bi.getRGB(x, y) != color.getRGB()) {
                System.err.println("Incorrect pixel at " + x + "x" + y + " : " + Integer.toHexString(bi.getRGB(x, y)) + " ,expected : " + Integer.toHexString(color.getRGB()));
                passed = false;
                return;
            }
        }
    }
}
Also used : Robot(java.awt.Robot) BufferedImage(java.awt.image.BufferedImage) AWTException(java.awt.AWTException)

Example 5 with AWTException

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

the class OnScreenRenderingResizeTest method main.

public static void main(String[] args) {
    for (String arg : args) {
        if ("-inf".equals(arg)) {
            System.err.println("Test will run indefinitely");
            RUN_TIME = Long.MAX_VALUE;
        } else if ("-nocheck".equals(arg)) {
            System.err.println("Test will not check rendering results");
            nocheck = true;
        } else {
            System.err.println("Usage: OnScreenRenderingResizeTest [-inf][-nocheck]");
        }
    }
    BufferedImage output = new BufferedImage(IMAGE_W, IMAGE_H, BufferedImage.TYPE_INT_RGB);
    output.setAccelerationPriority(0.0f);
    Graphics g = output.getGraphics();
    g.setColor(renderColor);
    g.fillRect(0, 0, output.getWidth(), output.getHeight());
    final Frame frame = new Frame("OnScreenRenderingResizeTest") {

        public void paint(Graphics g) {
        }

        public void update(Graphics g) {
        }
    };
    frame.setBackground(bgColor);
    frame.setUndecorated(true);
    frame.pack();
    GraphicsConfiguration gc = frame.getGraphicsConfiguration();
    Rectangle gcBounds = gc.getBounds();
    frame.setBounds(gcBounds.width / 4, gcBounds.height / 4, FRAME_W, FRAME_H);
    frame.addWindowListener(new WindowAdapter() {

        public void windowClosing(WindowEvent e) {
            done = true;
        }
    });
    try {
        EventQueue.invokeAndWait(new Runnable() {

            public void run() {
                frame.setVisible(true);
            }
        });
        // wait for Vista's effects to complete
        Thread.sleep(2000);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    int maxW = gcBounds.width / 2;
    int maxH = gcBounds.height / 2;
    int minW = frame.getWidth();
    int minH = frame.getHeight();
    int incW = 10, incH = 10, cnt = 0;
    Robot robot = null;
    if (!nocheck && gc.getColorModel().getPixelSize() > 8) {
        try {
            robot = new Robot();
        } catch (AWTException ex) {
            System.err.println("Robot creation failed, continuing.");
        }
    } else {
        System.err.println("No screen rendering checks.");
    }
    VolatileImage vi = gc.createCompatibleVolatileImage(512, 512);
    vi.validate(gc);
    long timeStarted = System.currentTimeMillis();
    while (!done && (System.currentTimeMillis() - timeStarted) < RUN_TIME) {
        if (++cnt > 100) {
            int w = frame.getWidth() + incW;
            int h = frame.getHeight() + incH;
            if (w < minW || w > maxW) {
                incW = -incW;
            }
            if (h < minH || h > maxH) {
                incH = -incH;
            }
            frame.setSize(w, h);
            cnt = 0;
        }
        // try to put the device into non-default state, for example,
        // this operation below will set the transform
        vi.validate(gc);
        Graphics2D vig = (Graphics2D) vi.getGraphics();
        vig.rotate(30.0f, vi.getWidth() / 2, vi.getHeight() / 2);
        vig.drawImage(output, 0, 0, vi.getWidth(), vi.getHeight(), null);
        Insets in = frame.getInsets();
        frame.getGraphics().drawImage(output, in.left, in.top, null);
        if (cnt == 90 && robot != null) {
            robot.waitForIdle();
            // area where we blitted to should be either white or green
            Point p = frame.getLocationOnScreen();
            p.translate(in.left + 10, in.top + 10);
            BufferedImage bi = robot.createScreenCapture(new Rectangle(p.x, p.y, IMAGE_W / 2, IMAGE_H / 2));
            int[] accepted1 = { Color.white.getRGB(), Color.green.getRGB() };
            checkBI(bi, accepted1);
            // the are where we didn't render should stay white
            p = frame.getLocationOnScreen();
            p.translate(in.left, in.top + IMAGE_H + 5);
            bi = robot.createScreenCapture(new Rectangle(p.x, p.y, frame.getWidth() - in.left - in.right, frame.getHeight() - in.top - in.bottom - 5 - IMAGE_H));
            int[] accepted2 = { Color.white.getRGB() };
            checkBI(bi, accepted2);
        }
        Thread.yield();
    }
    frame.dispose();
    System.out.println("Test Passed");
}
Also used : Frame(java.awt.Frame) Insets(java.awt.Insets) Rectangle(java.awt.Rectangle) WindowAdapter(java.awt.event.WindowAdapter) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) AWTException(java.awt.AWTException) Point(java.awt.Point) GraphicsConfiguration(java.awt.GraphicsConfiguration) Graphics2D(java.awt.Graphics2D) Graphics(java.awt.Graphics) VolatileImage(java.awt.image.VolatileImage) WindowEvent(java.awt.event.WindowEvent) 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