publicstatic Point getTitlePoint(Window decoratedWindow) {
Point p = decoratedWindow.getLocationOnScreen();
Dimension d = decoratedWindow.getSize(); returnnew Point(p.x + (int)(d.getWidth()/2),
p.y + (int)(decoratedWindow.getInsets().top/2));
}
/* *ClicksonatitleofFrame/Dialog. *WARNING:itmayfailonsomeplatformswhenthewindowisnotwideenough.
*/ publicstaticvoid 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);
}
}
/** *Testswhetherscreenpixelhastheexpectedcolorperformingseveral *attempts.Thismethodisusefulforasynchronouswindowmanagerwhere *it'simpossibletodeterminewhendrawingactuallytakesplace. * *@paramxXpositionofpixel *@paramyYpositionofpixel *@paramcolorexpectedcolor *@paramattemptsnumberofattemptstoundertake *@paramdelaydelaybeforeeachattempt *@paramrobotarobottouseforretrievingpixelcolor *@returntrueifpixelcolormatchesthecolorexpected,otherwisefalse
*/ publicstaticboolean 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)) { returntrue;
}
} returnfalse;
}
/** *Testswhethertheareawithinboundarieshastheexpectedcolor *performingseveralattempts.Thismethodisusefulforasynchronous *windowmanagerwhereit'simpossibletodeterminewhendrawingactually *takesplace. * *@paramboundspositionofarea *@paramcolorexpectedcolor *@paramattemptsnumberofattemptstoundertake *@paramdelaydelaybeforeeachattempt *@paramrobotarobottouseforretrievingpixelcolor *@returntrueifareacolormatchesthecolorexpected,otherwisefalse
*/ publicstaticboolean testBoundsColor(final Rectangle bounds, final Color color, int attempts, int delay, final Robot robot) { int right = bounds.x + bounds.width - 1; int bottom = bounds.y + bounds.height - 1; while (attempts-- > 0) { if (testPixelColor(bounds.x, bounds.y, color, 1, delay, robot)
&& testPixelColor(right, bounds.y, color, 1, 0, robot)
&& testPixelColor(right, bottom, color, 1, 0, robot)
&& testPixelColor(bounds.x, bottom, color, 1, 0, robot)) { returntrue;
}
} returnfalse;
}
/** *Movesthemousepointerfromonepointtoanother. *UsesBresenham'salgorithm. * *@paramrobotarobottouseformovingthemouse *@paramstartPointastartpointofthedrag *@paramendPointanendpointofthedrag
*/ publicstaticvoid mouseMove(Robot robot, Point startPoint, Point endPoint) { int dx = endPoint.x - startPoint.x; int dy = endPoint.y - startPoint.y;
int ax = Math.abs(dx) * 2; int ay = Math.abs(dy) * 2;
int sx = signWOZero(dx); int sy = signWOZero(dy);
int x = startPoint.x; int y = startPoint.y;
int d = 0;
if (ax > ay) {
d = ay - ax/2; while (true){
robot.mouseMove(x, y);
robot.delay(50);
if (x == endPoint.x){ return;
} if (d >= 0){
y = y + sy;
d = d - ax;
}
x = x + sx;
d = d + ay;
}
} else {
d = ax - ay/2; while (true){
robot.mouseMove(x, y);
robot.delay(50);
if (y == endPoint.y){ return;
} if (d >= 0){
x = x + sx;
d = d - ay;
}
y = y + sy;
d = d + ax;
}
}
}
privatestaticint signWOZero(int i){ return (i > 0)? 1: -1;
}
privatestaticint sign(int n) { return n < 0 ? -1 : n == 0 ? 0 : 1;
}
/* *TracksWINDOW_GAINED_FOCUSeventforawindowcausedbyanaction. *@paramwindowthewindowtotracktheeventfor *@paramactiontheactiontoperform *@paramtimethemaxtimetowaitfortheevent *@paramprintEventshouldtheeventreceivedbeprintedordoesn't *@returntrueiftheeventhasbeenreceived,otherwisefalse
*/ publicstaticboolean trackWindowGainedFocus(Window window, Runnable action, int time, boolean printEvent) { return trackEvent(WindowEvent.WINDOW_GAINED_FOCUS, window, action, time, printEvent);
}
/* *TracksFOCUS_GAINEDeventforacomponentcausedbyanaction. *@see#trackWindowGainedFocus
*/ publicstaticboolean trackFocusGained(Component comp, Runnable action, int time, booleanprintEvent) { return trackEvent(FocusEvent.FOCUS_GAINED, comp, action, time, printEvent);
}
/* *TracksACTION_PERFORMEDeventforabuttoncausedbyanaction. *@see#trackWindowGainedFocus
*/ publicstaticboolean trackActionPerformed(Button button, Runnable action, int time, boolean printEvent) { return trackEvent(ActionEvent.ACTION_PERFORMED, button, action, time, printEvent);
}
/* *Requestsfocusonthecomponentprovidedandwaitsfortheresult. *@returntrueifthecomponenthasbeenfocused,falseotherwise.
*/ publicstaticboolean focusComponent(Component comp, int time) { return focusComponent(comp, time, false);
} publicstaticboolean focusComponent(final Component comp, int time, boolean printEvent) { return trackFocusGained(comp, new Runnable() { publicvoid run() {
comp.requestFocus();
}
},
time, printEvent);
}
/** *Invokesthe<code>task</code>ontheEDTthread. * *@returnresultofthe<code>task</code>
*/ publicstatic <T> T invokeOnEDT(final java.util.concurrent.Callable<T> task) throws Exception { final java.util.List<T> result = new java.util.ArrayList<T>(1); final Exception[] exception = new Exception[1];
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.