public static void main(String[] args) throws Exception { UIManager.setLookAndFeel(new NimbusLookAndFeel()); Robot robot = new Robot(); robot.setAutoDelay(50); SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); SwingUtilities.invokeAndWait( new Runnable() { public void run() { createAndShowGUI(); } }); toolkit.realSync(); SwingUtilities.invokeAndWait( new Runnable() { public void run() { Component previewPanel = Util.findSubComponent(cc, "javax.swing.colorchooser.DefaultPreviewPanel"); point = previewPanel.getLocationOnScreen(); } }); point.translate(5, 5); robot.mouseMove(point.x, point.y); robot.mousePress(InputEvent.BUTTON1_MASK); robot.mouseRelease(InputEvent.BUTTON1_MASK); }
// 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(); } }
// test BUTTON1, 2 and 3 without any modifiers keys public static void testPlainButtons() { System.out.println("Testing buttons without modifiers."); f.addMouseListener(adapterTest1); for (int button : mouseButtons) { robot.mousePress(button); robot.delay(100); robot.mouseRelease(button); } robot.delay(1000); f.removeMouseListener(adapterTest1); }
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); }
/** Do screen capture and save it as image */ private static void captureScreenAndSave() { try { Robot robot = new Robot(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Rectangle rectangle = new Rectangle(0, 0, screenSize.width, screenSize.height); System.out.println("About to screen capture - " + rectangle); java.awt.image.BufferedImage image = robot.createScreenCapture(rectangle); javax.imageio.ImageIO.write(image, "jpg", new java.io.File("ScreenImage.jpg")); robot.delay(3000); } catch (Throwable t) { System.out.println("WARNING: Exception thrown while screen capture!"); t.printStackTrace(); } }
private void moveMousePointerOnButton(final JButton button) { Application application = ApplicationManager.getApplication(); if (application != null && application.hasComponent(UISettings.class)) { if (button != null && UISettings.getInstance().MOVE_MOUSE_ON_DEFAULT_BUTTON) { Point p = button.getLocationOnScreen(); Rectangle r = button.getBounds(); try { Robot robot = new Robot(); robot.mouseMove(p.x + r.width / 2, p.y + r.height / 2); } catch (AWTException e) { LOG.warn(e); } } } }
public static void main(String[] s) { JFrame frame = new JFrame("A test frame"); JPanel outputBox = new JPanel(); JButton jButton = new JButton(); frame.setSize(200, 200); frame.addMouseWheelListener( new MouseWheelListener() { public void mouseWheelMoved(MouseWheelEvent e) { System.out.println("Wheel moved on FRAME : " + e); actualEvents++; } }); outputBox.addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent e) { System.out.println("MousePressed on OUTBOX : " + e); } }); frame.add(outputBox); outputBox.add(jButton); frame.setVisible(true); Util.waitForIdle(robot); Util.pointOnComp(jButton, robot); Util.waitForIdle(robot); for (int i = 0; i < MOVE_COUNT; i++) { robot.mouseWheel(1); robot.delay(10); } for (int i = 0; i < MOVE_COUNT; i++) { robot.mouseWheel(-1); robot.delay(10); } Util.waitForIdle(robot); // Not fair to check for multiplier 4 as it's not specified actual number of WheelEvents // result in a single wheel rotation. if (actualEvents != EXPECTED_COUNT) { AbstractTest.fail( "Expected events count: " + EXPECTED_COUNT + " Actual events count: " + actualEvents); } }
/** * Release a specific key using its keycode. * * @param keycode the Java keycode, all available keycode can be found in java.awt.event.KeyEvent * class (VK_A, VK_SPACE, ...) * @see java.awt.event.KeyEvent */ public void keyRelease(int keycode) { /* AltGr does not seems to work with robot, handle it via our * JNI code */ if (keycode == KeyEvent.VK_ALT_GRAPH) { symbolRelease("altgr"); } else { robot.keyRelease(keycode); } }
public static void main(String[] args) throws Throwable { Robot robot = new Robot(); robot.setAutoDelay(250); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); SwingUtilities.invokeAndWait( new Runnable() { @Override public void run() { JFrame frame = new JFrame("Test"); frame.setContentPane(createPanel(frame)); frame.setJMenuBar(createMenuBar()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }); robot.waitForIdle(); // Change the default button to // force a call to BasicRootPaneUI.updateDefaultButtonBindings() Util.hitKeys(robot, KeyEvent.VK_TAB); // If the bug exists, then as soon as the menu appears, // the VK_ENTER, VK_DOWN, VK_UP and VK_ESC will have no // effect. Util.hitMnemonics(robot, KeyEvent.VK_U); Util.hitKeys(robot, KeyEvent.VK_ENTER); robot.waitForIdle(); checkAction(); Util.hitMnemonics(robot, KeyEvent.VK_U); Util.hitKeys(robot, KeyEvent.VK_DOWN); Util.hitKeys(robot, KeyEvent.VK_ENTER); robot.waitForIdle(); checkAction(); }
/** * Release a specific key using its char representation. * * @param key char representation of the key */ public void keyRelease(char key) { /* check for CTRL+X where X is [A-Z] * CTRL+A = 1, A = 65 */ if (key >= 1 && key <= 0x1A) { key = (char) (key + 64); robot.keyRelease(key); return; } if (nativeKeyboard != null) nativeKeyboard.keyRelease(key); }
/** * Press a specific key using its char representation. * * @param key char representation of the key */ public void keyPress(char key) { /* check for CTRL+X where X is [A-Z] * CTRL+A = 1, A = 65 */ if (key >= 1 && key <= 0x1A) { key = (char) (key + 64); robot.keyPress(key); return; } nativeKeyboard.keyPress(key); }
// recenter the mouse using the robot public synchronized void recenterMouse() { Window w = s.getFullScreenWindow(); if (robot != null && w.isShowing()) { center.x = w.getWidth() / 2; center.y = w.getHeight() / 2; SwingUtilities.convertPointToScreen(center, w); centering = true; robot.mouseMove(center.x, center.y); } }
public void init() { try { robot = new Robot(); robot.setAutoDelay(50); } catch (AWTException e) { throw new RuntimeException("Error: unable to create robot", e); } // Create instructions for the user here, as well as set up // the environment -- set the layout manager, add buttons, // etc. this.setLayout(new BorderLayout()); Sysout.createDialogWithInstructions( new String[] {"Automatic test. Simply wait until it's done."}); }
public static void main(String s[]) { initParams(s); initAdapters(); f = new Frame(); final int[] modifiers = {InputEvent.SHIFT_MASK, InputEvent.CTRL_MASK}; final String[] modifierNames = {"InputEvent.SHIFT_MASK", "InputEvent.CTRL_MASK"}; f.setLayout(new FlowLayout()); f.addMouseWheelListener( new MouseWheelListener() { public void mouseWheelMoved(MouseWheelEvent e) { System.out.println("WHEEL " + e); } }); f.setSize(300, 300); f.setVisible(true); try { robot = new Robot(); robot.delay(500); robot.mouseMove( f.getLocationOnScreen().x + f.getWidth() / 2, f.getLocationOnScreen().y + f.getHeight() / 2); if (autorun) { // testing buttons 1, 2, 3 only testPlainButtons(); robot.delay(500); // testing buttons 1, 2, 3 with SHIFT, CTRL, ALT keyboard modifiers testButtonsWithShift(); robot.delay(500); testButtonsWithControl(); robot.delay(500); testButtonsWithAlt(); robot.delay(500); } else { switch (testModifier) { case SHIFT: f.addMouseListener(adapterTest2); break; case CTRL: f.addMouseListener(adapterTest3); break; case ALT: f.addMouseListener(adapterTest4); break; default: // NONE inclusive f.addMouseListener(adapterTest1); } } } catch (Exception e) { throw new RuntimeException("Test failed."); } }
// test BUTTON1, 2 and 3 with ALT key public static void testButtonsWithAlt() { System.out.println("Testing buttons with ALT modifier."); f.addMouseListener(adapterTest4); for (int button : mouseButtons) { robot.keyPress(KeyEvent.VK_ALT); robot.mousePress(button); robot.delay(100); robot.mouseRelease(button); robot.keyRelease(KeyEvent.VK_ALT); } robot.delay(1000); f.removeMouseListener(adapterTest4); }
/** * Press a specific key using its keycode. * * @param keycode the Java keycode, all available keycodes can be found in java.awt.event.KeyEvent * class (VK_A, VK_SPACE, ...) * @see java.awt.event.KeyEvent */ public void keyPress(int keycode) { if (OSUtils.IS_WINDOWS || OSUtils.IS_MAC) { /* do not allow modifiers for Windows (as * they are handled in native code with * VkScanCode) and Mac OS X */ if (keycode == KeyEvent.VK_ALT || keycode == KeyEvent.VK_SHIFT || keycode == KeyEvent.VK_ALT_GRAPH) { return; } } /* AltGr does not seems to work with robot, handle it via our * JNI code */ if (keycode == KeyEvent.VK_ALT_GRAPH) { symbolPress("altgr"); } else { robot.keyPress(keycode); } }
public void start() { menu.setMnemonic('f'); submenu.setMnemonic('m'); menu.add(submenu); submenu.add(item); bar.add(menu); frame.setJMenuBar(bar); frame.pack(); item.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { Sysout.println(e.toString()); synchronized (activated) { activated.set(true); activated.notifyAll(); } } }); frame.setVisible(true); Util.waitForIdle(robot); boolean isMacOSX = (OSInfo.getOSType() == OSInfo.OSType.MACOSX); if (isMacOSX) { robot.keyPress(KeyEvent.VK_CONTROL); robot.delay(20); } robot.keyPress(KeyEvent.VK_ALT); robot.delay(20); robot.keyPress(KeyEvent.VK_F); robot.delay(20); robot.keyRelease(KeyEvent.VK_F); robot.delay(20); robot.keyRelease(KeyEvent.VK_ALT); if (isMacOSX) { robot.keyRelease(KeyEvent.VK_CONTROL); robot.delay(20); } Util.waitForIdle(robot); robot.keyPress(KeyEvent.VK_M); robot.delay(20); robot.keyRelease(KeyEvent.VK_M); Util.waitForIdle(robot); robot.keyPress(KeyEvent.VK_SPACE); robot.delay(20); robot.keyRelease(KeyEvent.VK_SPACE); Util.waitForIdle(robot); if (!Util.waitForCondition(activated, 2000)) { throw new TestFailedException("a submenu wasn't activated by mnemonic key press"); } Sysout.println("Test passed."); }
/** * Runs a sample test procedure * * @param robot the robot attached to the screen device */ public static void runTest(Robot robot) { // simulate a space bar press robot.keyPress(' '); robot.keyRelease(' '); // simulate a tab key followed by a space robot.delay(2000); robot.keyPress(KeyEvent.VK_TAB); robot.keyRelease(KeyEvent.VK_TAB); robot.keyPress(' '); robot.keyRelease(' '); // simulate a mouse click over the rightmost button robot.delay(2000); robot.mouseMove(200, 50); robot.mousePress(InputEvent.BUTTON1_MASK); robot.mouseRelease(InputEvent.BUTTON1_MASK); // capture the screen and show the resulting image robot.delay(2000); BufferedImage image = robot.createScreenCapture(new Rectangle(0, 0, 400, 300)); ImageFrame frame = new ImageFrame(image); frame.setVisible(true); }
public static void main(String[] args) throws Exception { Robot robot = new Robot(); robot.setAutoDelay(350); SwingUtilities.invokeAndWait( new Runnable() { public void run() { createAndShowGUI(); } }); robot.waitForIdle(); SwingUtilities.invokeAndWait( new Runnable() { public void run() { spane.requestFocus(); sbar.setValue(sbar.getMaximum()); } }); robot.waitForIdle(); Point point = getClickPoint(0.5, 0.5); robot.mouseMove(point.x, point.y); robot.mousePress(InputEvent.BUTTON1_MASK); robot.waitForIdle(); SwingUtilities.invokeAndWait( new Runnable() { public void run() { final int oldValue = sbar.getValue(); sbar.addAdjustmentListener( new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent e) { if (e.getValue() >= oldValue) { passed = false; } do_test = true; } }); } }); robot.waitForIdle(); point = getClickPoint(0.5, 0.2); robot.mouseMove(point.x, point.y); robot.mouseRelease(InputEvent.BUTTON1_MASK); robot.waitForIdle(); if (!do_test || !passed) { throw new Exception("The scrollbar moved with incorrect direction"); } }
/** * Press a mouse button(s). * * @param btns button masks * @see java.awt.Robot#mousePress(int btns) */ public void mousePress(int btns) { robot.mousePress(btns); }
void test() { robot.waitForIdle(); if (!text.isFocusOwner()) { robot.mouseMove(text.getLocationOnScreen().x + 5, text.getLocationOnScreen().y + 5); robot.delay(100); robot.mousePress(MouseEvent.BUTTON1_MASK); robot.delay(100); robot.mouseRelease(MouseEvent.BUTTON1_MASK); int iter = 10; while (!text.isFocusOwner() && iter-- > 0) { robot.delay(200); } if (iter <= 0) { Sysout.println("Test: text field couldn't be focused!"); return; } } robot.keyPress(KeyEvent.VK_A); robot.delay(100); robot.keyRelease(KeyEvent.VK_A); robot.waitForIdle(); String charA = text.getText(); System.err.println("Test: character typed with VK_A: " + charA); robot.keyPress(KeyEvent.VK_BACK_SPACE); robot.delay(100); robot.keyRelease(KeyEvent.VK_BACK_SPACE); robot.waitForIdle(); if (jdk.testlibrary.OSInfo.getOSType() == jdk.testlibrary.OSInfo.OSType.MACOSX) { robot.keyPress(KeyEvent.VK_CONTROL); } robot.keyPress(KeyEvent.VK_ALT); robot.keyPress(KeyEvent.VK_F); robot.delay(100); robot.keyRelease(KeyEvent.VK_F); robot.keyRelease(KeyEvent.VK_ALT); if (jdk.testlibrary.OSInfo.getOSType() == jdk.testlibrary.OSInfo.OSType.MACOSX) { robot.keyRelease(KeyEvent.VK_CONTROL); } robot.waitForIdle(); String string = text.getText(); robot.keyPress(KeyEvent.VK_I); robot.delay(100); robot.keyRelease(KeyEvent.VK_I); robot.waitForIdle(); Sysout.println("Test: character typed after mnemonic key press: " + text.getText()); if (!text.getText().equals(string)) { throw new RuntimeException("Test failed!"); } robot.keyPress(KeyEvent.VK_A); robot.delay(100); robot.keyRelease(KeyEvent.VK_A); robot.waitForIdle(); System.err.println("Test: chracter typed with VK_A: " + text.getText()); if (!charA.equals(text.getText())) { throw new RuntimeException("Test failed!"); } Sysout.println("Test passed."); }
public void takeScreenshot(boolean flag) { BufferedImage bufferedimage; try { Robot robot = new Robot(); Point point = getLocationOnScreen(); Rectangle rectangle = new Rectangle(point.x, point.y, getWidth(), getHeight()); bufferedimage = robot.createScreenCapture(rectangle); } catch (Throwable throwable) { JOptionPane.showMessageDialog( frame, "An error occured while trying to create a screenshot!", "Screenshot Error", 0); return; } String s = null; try { s = getNearestScreenshotFilename(); } catch (IOException ioexception) { if (flag) { JOptionPane.showMessageDialog( frame, "A screenshot directory does not exist, and could not be created!", "No Screenshot Directory", 0); return; } } if (s == null && flag) { JOptionPane.showMessageDialog( frame, "There are too many screenshots in the screenshot directory!\n" + "Delete some screen\n" + "shots and try again.", "Screenshot Directory Full", 0); return; } if (!flag) { final JFileChooser fileChooser = new JFileChooser(); final JDialog fileDialog = createFileChooserDialog(fileChooser, "Save Screenshot", this); final BufferedImage si = bufferedimage; JFileChooser _tmp = fileChooser; fileChooser.setFileSelectionMode(0); fileChooser.addChoosableFileFilter(new imageFileFilter()); fileChooser.setCurrentDirectory(new File("C:/.hack3rClient/Scre/")); fileChooser.setSelectedFile(new File(s)); JFileChooser _tmp1 = fileChooser; fileChooser.setDialogType(1); fileChooser.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent actionevent) { String s1 = actionevent.getActionCommand(); if (s1.equals("ApproveSelection")) { File file = fileChooser.getSelectedFile(); if (file != null && file.isFile()) { int i = JOptionPane.showConfirmDialog( frame, (new StringBuilder()) .append(file.getAbsolutePath()) .append(" already exists.\n" + "Do you want to replace it?") .toString(), "Save Screenshot", 2); if (i != 0) { return; } } try { ImageIO.write(si, "png", file); // client.pushMessage("Screenshot taken.", 3, "@cr2@ Client"); // JOptionPane.showMessageDialog(frame,"Screenshot Taken"); } catch (IOException ioexception2) { JOptionPane.showMessageDialog( frame, "An error occured while trying to save the screenshot!\n" + "Please make sure you have\n" + " write access to the screenshot directory.", "Screenshot Error", 0); } fileDialog.dispose(); } else if (s1.equals("CancelSelection")) { fileDialog.dispose(); } } { } }); fileDialog.setVisible(true); } else { try { ImageIO.write( bufferedimage, "png", new File((new StringBuilder()).append("C:/.hack3rClient/Scre/").append(s).toString())); JOptionPane.showMessageDialog( frame, "Image has been saved to C:/.hack3rclient/Scre. You can view your images by pushing File>View Images in the menu dropdowns.", "Screenshot manager", JOptionPane.INFORMATION_MESSAGE); // client.pushMessage("Screenshot taken.", 3, "@cr2@ Client"); // JOptionPane.showMessageDialog(frame,"Screenshot taken."); } catch (IOException ioexception1) { JOptionPane.showMessageDialog( frame, "An error occured while trying to save the screenshot!\n" + "Please make sure you have\n" + " write access to the screenshot directory.", "Screenshot Error", 0); } } }
public static void main(String[] args) throws Throwable { sun.awt.SunToolkit toolkit = (sun.awt.SunToolkit) Toolkit.getDefaultToolkit(); SwingUtilities.invokeAndWait( new Runnable() { public void run() { test = new TaskbarPositionTest(); } }); // Use Robot to automate the test Robot robot; robot = new Robot(); robot.setAutoDelay(125); // 1 - menu Util.hitMnemonics(robot, KeyEvent.VK_1); toolkit.realSync(); isPopupOnScreen(menu1.getPopupMenu(), screenBounds); // 2 menu with sub menu robot.keyPress(KeyEvent.VK_RIGHT); robot.keyRelease(KeyEvent.VK_RIGHT); Util.hitMnemonics(robot, KeyEvent.VK_S); toolkit.realSync(); isPopupOnScreen(menu2.getPopupMenu(), screenBounds); robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); // Focus should go to non editable combo box toolkit.realSync(); Thread.sleep(500); robot.keyPress(KeyEvent.VK_DOWN); // How do we check combo boxes? // Editable combo box robot.keyPress(KeyEvent.VK_TAB); robot.keyRelease(KeyEvent.VK_TAB); robot.keyPress(KeyEvent.VK_DOWN); robot.keyRelease(KeyEvent.VK_DOWN); // combo1.getUI(); // Popup from Text field robot.keyPress(KeyEvent.VK_TAB); robot.keyRelease(KeyEvent.VK_TAB); robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_DOWN); robot.keyRelease(KeyEvent.VK_DOWN); robot.keyRelease(KeyEvent.VK_CONTROL); // Popup from a mouse click. Point pt = new Point(2, 2); SwingUtilities.convertPointToScreen(pt, panel); robot.mouseMove((int) pt.getX(), (int) pt.getY()); robot.mousePress(InputEvent.BUTTON3_MASK); robot.mouseRelease(InputEvent.BUTTON3_MASK); toolkit.realSync(); SwingUtilities.invokeAndWait( new Runnable() { public void run() { test.setLocation(-30, 100); combo1.addPopupMenuListener(new ComboPopupCheckListener()); combo1.requestFocus(); } }); robot.keyPress(KeyEvent.VK_DOWN); robot.keyRelease(KeyEvent.VK_DOWN); robot.keyPress(KeyEvent.VK_ESCAPE); robot.keyRelease(KeyEvent.VK_ESCAPE); toolkit.realSync(); Thread.sleep(500); }
/** * Release a mouse button(s). * * @param btns button masks * @see java.awt.Robot#mouseRelease(int btns) */ public void mouseRelease(int btns) { robot.mouseRelease(btns); }
/** * Move the mouse on the screen. * * @param x x position on the screen * @param y y position on the screen * @see java.awt.Robot#mouseMove(int x, int y) */ public void mouseMove(int x, int y) { robot.mouseMove(x, y); }
/** * Release a mouse button(s). * * @param rotation wheel rotation (could be negative or positive depending on the direction). * @see java.awt.Robot#mouseWheel(int wheelAmt) */ public void mouseWheel(int rotation) { robot.mouseWheel(rotation); }
public ScreenShotWindow() throws AWTException { // 获取屏幕尺寸 Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); this.setBounds(0, 0, d.width, d.height); // 截取屏幕 Robot robot = new Robot(); image = robot.createScreenCapture(new Rectangle(0, 0, d.width, d.height)); this.addMouseListener( new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { // 鼠标松开时记录结束点坐标,并隐藏操作窗口 orgx = e.getX(); orgy = e.getY(); if (tools != null) { tools.setVisible(false); } } @Override public void mouseReleased(MouseEvent e) { // 鼠标松开时,显示操作窗口 if (tools == null) { tools = new ToolsWindow(ScreenShotWindow.this, e.getX(), e.getY()); } else { tools.setLocation(e.getX(), e.getY()); } tools.setVisible(true); tools.toFront(); } }); this.addMouseMotionListener( new MouseMotionAdapter() { @Override public void mouseDragged(MouseEvent e) { // 鼠标拖动时,记录坐标并重绘窗口 endx = e.getX(); endy = e.getY(); // 临时图像,用于缓冲屏幕区域放置屏幕闪烁 Image tempImage2 = createImage(ScreenShotWindow.this.getWidth(), ScreenShotWindow.this.getHeight()); Graphics g = tempImage2.getGraphics(); g.drawImage(tempImage, 0, 0, null); int x = Math.min(orgx, endx); int y = Math.min(orgy, endy); int width = Math.abs(endx - orgx) + 1; int height = Math.abs(endy - orgy) + 1; // 加上1防止width或height0 g.setColor(Color.BLUE); g.drawRect(x - 1, y - 1, width + 1, height + 1); // 减1加1都了防止图片矩形框覆盖掉 saveImage = image.getSubimage(x, y, width, height); g.drawImage(saveImage, x, y, null); ScreenShotWindow.this.getGraphics().drawImage(tempImage2, 0, 0, ScreenShotWindow.this); } }); }
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."); } }