下面列出了java.awt.Robot#delay ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void moveTo(
Robot r,
Point b,
Point e)
{
Point2D.Double ee = new Point2D.Double(e.getX(), e.getY());
Point2D.Double bb = new Point2D.Double(b.getX(), b.getY());
final int count = (int)(ee.distance(bb));
Point2D.Double c = new Point2D.Double(bb.getX(), bb.getY());
for(int i=0; i<count; ++i){
c.setLocation(
bb.getX() + (ee.getX()-bb.getX())*i/count,
bb.getY() + (ee.getY()-bb.getY())*i/count);
r.mouseMove(
(int)c.getX(),
(int)c.getY());
r.delay(5);
}
r.mouseMove(
(int)ee.getX(),
(int)ee.getY());
r.delay(5);
}
public static void main(String[] args) throws Exception {
robot = new Robot();
UIManager.LookAndFeelInfo[] lookAndFeelArray
= UIManager.getInstalledLookAndFeels();
for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {
String lookAndFeelString = lookAndFeelItem.getClassName();
if (tryLookAndFeel(lookAndFeelString)) {
createUI();
robot.waitForIdle();
executeTest();
robot.delay(1000);
}
}
if (!"".equals(errorMessage)) {
throw new RuntimeException(errorMessage);
}
}
public void init() throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
editorPane = new JEditorPane("text/html", "");
editorPane.setEditable(false);
editorPane.setMargin(new java.awt.Insets(0, 0, 0, 0));
editorPane.setText(text);
f = new JFrame();
f.getContentPane().add(editorPane);
f.setSize(600, 400);
f.setVisible(true);
}
});
blockTillDisplayed(editorPane);
Robot robot = new Robot();
robot.waitForIdle();
robot.delay(300);
int x0 = p.x + 15 ;
int y = p.y + 15;
int match = 0;
int nonmatch = 0;
passed = true;
for (int x = x0; x < x0 + 10; x++) {
System.out.println("color ("+x+"," + y +")=" + robot.getPixelColor(x,y));
if (!robot.getPixelColor(x, y).equals(new Color(0xcc, 0xcc, 0xcc))) {
nonmatch++;
} else match++;
}
if (nonmatch > match) {
passed = false;
}
}
public static void clickOnTitle(final Window decoratedWindow, final Robot robot) {
if (decoratedWindow instanceof Frame || decoratedWindow instanceof Dialog) {
Point p = getTitlePoint(decoratedWindow);
robot.mouseMove(p.x, p.y);
robot.delay(50);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(50);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
}
static void mouseClickOnComp(Robot r, Component comp) {
Point loc = comp.getLocationOnScreen();
loc.x += comp.getWidth() / 2;
loc.y += comp.getHeight() / 2;
r.mouseMove(loc.x, loc.y);
r.delay(10);
r.mousePress(InputEvent.BUTTON1_MASK);
r.delay(10);
r.mouseRelease(InputEvent.BUTTON1_MASK);
}
public static void main(String[] args) throws Exception {
robot = new Robot();
robot.delay(1000);
UIManager.LookAndFeelInfo[] lookAndFeelArray
= UIManager.getInstalledLookAndFeels();
for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {
executeCase(lookAndFeelItem.getClassName(),
lookAndFeelItem.getName());
}
if (!"".equals(errorString)) {
throw new RuntimeException("Error Log:\n" + errorString);
}
}
public static void main(String[] args) throws Exception {
robot = new Robot();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
createUI();
}
});
robot.waitForIdle();
executeTest();
robot.delay(1000);
dispose();
}
public static void clickOnTitle(final Window decoratedWindow, final Robot robot) {
if (decoratedWindow instanceof Frame || decoratedWindow instanceof Dialog) {
Point p = getTitlePoint(decoratedWindow);
robot.mouseMove(p.x, p.y);
robot.delay(50);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(50);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
}
public static void clickOnTitle(final Window decoratedWindow, final Robot robot) {
if (decoratedWindow instanceof Frame || decoratedWindow instanceof Dialog) {
Point p = getTitlePoint(decoratedWindow);
robot.mouseMove(p.x, p.y);
robot.delay(50);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(50);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
}
public static void clickOnTitle(final Window decoratedWindow, final Robot robot) {
if (decoratedWindow instanceof Frame || decoratedWindow instanceof Dialog) {
Point p = getTitlePoint(decoratedWindow);
robot.mouseMove(p.x, p.y);
robot.delay(50);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(50);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
}
private static void rup(Robot robot) {
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(50);
}
private static void rdown(Robot robot) {
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(50);
}
private static void rup(Robot robot) {
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(50);
}
private static void rdown(Robot robot) {
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(50);
}
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!");
}
}
}
private static void rdown(Robot robot) {
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(50);
}
@Override
public void start() {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.err.println(e);
}
}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
boolean gained = false;
final Robot robot = Util.createRobot();
JFrame frame1 = new JFrame("Main Frame");
final JButton b1 = new JButton("button1");
frame1.add(b1);
frame1.pack();
frame1.setLocation(0, 300);
Util.showWindowWait(frame1);
final JFrame frame2 = new JFrame("Test Frame");
final JButton b2 = new JButton("button2");
frame2.add(b2);
frame2.pack();
frame2.setLocation(300, 300);
b2.setEnabled(false);
b2.requestFocus();
Util.showWindowWait(frame2);
robot.delay(500);
//
// It's expeced that the focus is restored to <button1>.
// If not, click <button1> to set focus on it.
//
if (!b1.hasFocus()) {
gained = Util.trackFocusGained(b1, new Runnable() {
public void run() {
Util.clickOnComp(b1, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Unexpected state: focus is not on <button1>");
}
}
robot.delay(500);
//
// Click <button2>, check that focus is set on the parent frame.
//
gained = false;
gained = Util.trackFocusGained(frame2, new Runnable() {
public void run() {
Util.clickOnComp(b2, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
}
System.out.println("Test passed.");
}
private static void rdown(Robot robot) {
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(50);
}
/**
* Tests whether screen pixel has the expected color performing several
* attempts. This method is useful for asynchronous window manager where
* it's impossible to determine when drawing actually takes place.
*
* @param x X position of pixel
* @param y Y position of pixel
* @param color expected color
* @param attempts number of attempts to undertake
* @param delay delay before each attempt
* @param robot a robot to use for retrieving pixel color
* @return true if pixel color matches the color expected, otherwise false
*/
public static boolean testPixelColor(int x, int y, final Color color, int attempts, int delay, final Robot robot) {
while (attempts-- > 0) {
robot.delay(delay);
Color screen = robot.getPixelColor(x, y);
if (screen.equals(color)) {
return true;
}
}
return false;
}
/**
* Tests whether screen pixel has the expected color performing several
* attempts. This method is useful for asynchronous window manager where
* it's impossible to determine when drawing actually takes place.
*
* @param x X position of pixel
* @param y Y position of pixel
* @param color expected color
* @param attempts number of attempts to undertake
* @param delay delay before each attempt
* @param robot a robot to use for retrieving pixel color
* @return true if pixel color matches the color expected, otherwise false
*/
public static boolean testPixelColor(int x, int y, final Color color, int attempts, int delay, final Robot robot) {
while (attempts-- > 0) {
robot.delay(delay);
Color screen = robot.getPixelColor(x, y);
if (screen.equals(color)) {
return true;
}
}
return false;
}